Posts

Showing posts from June, 2021

Simulating arduino and atmega

Image
 This is a simple update of the project I'm working. I am attempting to build a simulation model for an entire atmega mcu, that can be inegrated with ngspice (an open source circuit simulator). The idea to take the binary file in the .hex format , parse it to get the instructions and then execute the program instruction by instruction to get a response (similar to a processor emulator). In this post I am giving a demo of the work that has been done till now. I will in this post, demonstrate how I am able to get a pwm pulse from the MCU and use it to drive the motor. The system being simulated is as :  The c program used is as follows :  define F_CPU 1000000UL #include <xc.h> #include <avr/io.h> #include <util/delay.h> int main (){ DDRD = 0xFF ; TCCR0A = 0x81 ; TCCR0B = 0x01 ; OCR0A = 0xFE ; return 0 ; } From this I get a hex file using the xc8 compiler for avr. I use that file to get the set of instructions in the program a...

Counting pulses in Arduino (without interrupts or loops)

Image
 In this post, I will talk about how to count pulses in Arduino without using any interrupts or loops. This method can come in handy when the controller is handling a lot of task which can either cause it to miss some counts or introduce some unnecessary delays if some checks are included.  Even when using interrupts, it will have to interrupt a program in execution in order to increment a count. This will obviously cause delays for the rest of the program.  In this blog, I will be using the built in timer/counter module of Arduino for the purpose. More details about this module can be found in the atmega328p datasheet ( link ) (in chapter 14,15 and 16).  I will provide a basic introduction, mainly the three registers (TCCR0A, TCCR0B and TCNT0) that will be of use to us.  Introduction  A basic function of the timer counter is to (as the name suggests) "count". It increments the counter (the value in the TCNT0 register) whenever it encounters a clock. The cl...