Hi, I want to display the number stored in the register OCR1A, which is a number ranging from 800 to 2200. This a 50Hz PWM signal using Timer1. I am really starting to learn AVR so I'm no expert on this. I'm reading different sources and references about programming AVR so I can achieve what I am looking for. Below I post a code which I intended to display the value in OCR1. The four 7 segment displays are multiplexed in PORTC. This did not work. Can anyone explain me what is going wrong? I need to be able to show a value on the display so after that I want to add other features to my program, for example, incrementing/decrementing the value of OCR1A using push buttons with software deboucing.

This is the code: (By the way, I am programming an ATMEGA16)

#define F_CPU 8000000 // 8MHZ crystal #include <avr/io.h> #include <util/delay.h> #include <avr/interrupt.h> unsigned short digits[3]; unsigned short digit; void shownumber(unsigned char n) { /*This function writes a digits given by n to the display Note: n must be less than 9 */ if(n<10) { switch (n) { case 0: PORTC=0x3F; break; case 1: PORTC=0x06; break; case 2: PORTC=0x5B; break; case 3: PORTC=0x4F; break; case 4: PORTC=0x066; break; case 5: PORTC=0x6D; break; case 6: PORTC=0x7D; break; case 7: PORTC=0x07; break; case 8: PORTC=0x7F; break; case 9: PORTC=0x6F; break; } } } //-------------- Returns mask for common cathode 7-seg. display unsigned short mask(unsigned short num) { switch (num) { case 0 : return 0x3F; case 1 : return 0x06; case 2 : return 0x5B; case 3 : return 0x4F; case 4 : return 0x66; case 5 : return 0x6D; case 6 : return 0x7D; case 7 : return 0x07; case 8 : return 0x7F; case 9 : return 0x6F; } //case end } void splitn(unsigned short int number) { /* This function breaks apart a given integer into separate digits and writes them to the display array i.e. digits[] */ if (number>=1000) { digit = number / 1000; // extract thousands digit digits[3] = mask(digit); // and store it to port_array digit = (number / 100) % 10; // extract hundreds digit digits[2] = mask(digit); // and store it to port_array digit = (number / 10) % 10; // extract tens digit digits[1] = mask(digit); // and store it to port_array digit = number % 10; // extract ones digit digits[0] = mask(digit); // and store it to port_array } else { digit = (number / 100) % 10; // extract hundreds digit digits[2] = mask(digit); // and store it to port_array digit = (number / 10) % 10; // extract tens digit digits[1] = mask(digit); // and store it to port_array digit = number % 10; // extract ones digit digits[0] = mask(digit); // and store it to port_array } } int main(void){ DDRC = 0xFF; DDRB |= 0x0F; DDRD = 0xFF; digit = 0; // Initial PORTB = 0; // PORTC = 0; // TCCR1A |= 1<<WGM11 | 1<<COM1A1; TCCR1B |= 1<<WGM12 | 1<<WGM13 | 1<<CS11; ICR1=19999; //set ICR1 to produce 50Hz frequency cli(); TIMSK|=(1<<TOIE0); TCCR0|=(1<<CS00)|(1<<CS01); sei(); while (1){ OCR1A=1000; splitn(OCR1A); } } ISR(TIMER0_OVF_vect) { /* This interrupt service routine (ISR) Updates the displays */ static unsigned char i=0; if(i==3) { //If on last display then come //back to first. i=0; } else { //Goto Next display i++; } //Activate a display according to i PORTB&=(0b11110000); PORTB|=(1<<i); //Write the digit[i] in the ith display. shownumber(digits[i]); }