Port code for ATMega328 to ATMega8 (timer / counter) - Page 1
Forum section: electronics / general

author: daemon123
author_rank: Contributor
timestamp: February 03, 2017, 02:20:16 pm
content: Hi,found this Code, the orignal author is russian and so was the article. I have a spare ATMega8 and would like to put it on it. But the code uses timer/counter and PWM stuff, which is specific to ATMega328 family, can anyone help in porting it to ATMega8 specific timer/counters ?here is the Code:Code:// Soldering station "JAY-936"// Author - Chernyakov Sergey S. - [url=http://www.embed.com.ua]www.embed.com.ua// Connecting an external file#include <avr/io.h>#include <avr/interrupt.h>#include <util/delay.h>#include <avr/eeprom.h>#define DIG1 PB0 /*Cathode 1-st digit indicator */#define DIG2 PB6 /*Cathode 2-nd digit indicator*/#define DIG3 PB7 /*Cathode 3-rd digit indicator */#define DIG4 PD7 /*Cathode 4-th digit indicator */#define PORT_DIG1 PORTB /*Port cathode 1st digit indicator */#define PORT_DIG2 PORTB /*Port cathode 2st digit indicator*/#define PORT_DIG3 PORTB /*Port cathode 3st digit indicator */#define PORT_DIG4 PORTD /*Port cathode 4st digit indicator */#define BUZZER PB2 /*Output buzzer*/#define PORT_BUZZER PORTB /*Port buzzer*/#define DDR_BUZZER DDRB /*Register Direction buzzer*/#define LED PC4 /*Output LED*/#define PORT_LED PORTC /*Port LEDs*/#define DDR_LED DDRC /*Register LED direction*/#define BUTTON PC3 /*Login button*/#define PORT_BUTTON PORTC /*Port button*/#define DDR_BUTTON DDRC /*Register direction buttons*/#define PIN_BUTTON PINC /*Contact button*/#define ENC_0 PC0 /*0-its entrance ýnkodera*/#define ENC_1 PC1 /*1st encoder input*/#define PORT_ENC PORTC /*Port encoder*/#define DDR_ENC DDRC /*Register direction encoder inputs */#define PIN_ENC PINC /*Contact encoder inputs*/#define Vdd 4930 /*Supply voltage - reference for the ADC*/#define PI_PIP TCCR2B |= (1 << CS21) | (1 << CS20) /*Macro for buzzer (pi) beeping*/const unsigned char cifra [] = {0x77,0x44,0x5B,0x5E,0x6C,0x3E,0x3F,0x54,0x7F,0x7E};// An array of codes to form figuresunsigned char sot, des, edi, EncState;unsigned int *a;//A pointer to the display value (setting encoder / actual temperature)volatile unsigned int Temperature, time, EncData, pwm, status;enum {work, sleep};void podsvet (void) //Display values Function{static unsigned char q;//Indoor counter Display categoryq++;switch(q) //Bust discharge Display Now{case 1:PORT_DIG4 &= ~(1 << DIG4);//Gasim Previous rankPORTD &= 0x80; PORTD |= cifra;//Set the corresponding numberPORT_DIG1 |= (1 << DIG1);//Highlights by following dischargebreak;case 2:PORT_DIG1 &= ~(1 << DIG1);PORTD &= 0x80; PORTD |= cifra;PORT_DIG2 |= (1 << DIG2);break;case 3:PORT_DIG2 &= ~(1 << DIG2);PORTD &= 0x80; PORTD |= cifra;PORT_DIG3 |= (1 << DIG3);break;case 4:PORT_DIG3 &= ~(1 << DIG3);PORTD &= 0x80; PORTD |= 0b00101011;//In the fourth permanent illumination of the discharge letter t // cifra ;PORT_DIG4 |= (1 << DIG4);q=0;//As highlighted in the last 4-th bit counter resetbreak;default:break;}}void EncoderScan(void)//encoder processing function{unsigned char New;//Partly new value encoderNew = PIN_ENC & ((1<< ENC_0) | (1 << ENC_1));// Read the current position of the encoderif(New != EncState)//If the value has changed compared to the previous{switch(EncState) //Enumerating the last value encoder{case 2:if(New == 3) EncData++;//Depending on the increaseif(New == 0) EncData--;//Or reducebreak;case 0:if(New == 2) EncData++;if(New == 1) EncData--;break;case 1:if(New == 0) EncData++;if(New == 3) EncData--;break;case 3:if(New == 1) EncData++;if(New == 2) EncData--;break;default:break;}if(EncData > 360) EncData = 360;//Be careful not to go beyond the topif(EncData < 200) EncData = 200;//and lower limitEncState = New;// We write the new value of the previous statea = &EncData;//Pointer to assign an address encoder current value (set temperature)TCCR0B = TCNT0 = time = 0; //Timer 0 is stopped and resetTCCR0B |= (1 << CS02) | (1 << CS00);//And restart for reference 3 seconds and 15 minutesPI_PIP;//Pipiknut for confirmation hearing}}void A_D_C (void)//ADC service function{static unsigned long Usum;static unsigned int i;ADCSRA |=(1<<ADSC); // Run ADCwhile(!(ADCSRA & (1<<ADIF))); // Wait for the end of conversionADCSRA |=(1<<ADIF); // Reset completion flag conversionUsum += ADCW; //Summarize the ADC values to calculate the average (eliminating the "twitch" bits)if(i++ >= 300) //If a 300 times already measured{Temperature = (((Usum/300)*Vdd)/1023)/10;//Calculate the value of the temperatureUsum=i=0; //voltage and reset the counter}}ISR (TIMER0_OVF_vect) //Timer 0 - for the reference time interval (overflow ~ 32ms){static unsigned int p,l;//Counters for "baking" and flashing LED in sleep modeif (status == work)//If the status of the work{time++;//Increase the counter overflowsif(time < 91)//If less than 3 seconds after the installation of the encoder temperature{if(bit_is_clear(PIN_BUTTON, BUTTON)){eeprom_write_word(0x10, EncData);//when you press the button to record the value in the EEPROMPI_PIP;//and pipiknut}}if(time >= 91) //If it has been 3 seconds = 32 ms x 91a = &Temperature; //Pointer assign the address of the current tip temperatureif(time >= 28125) //If it's been 15 minutes{TCNT0 = 0; //Timer 0 resetTCCR1B = TCCR1A = 0;//PWM stoptime = 0;//Reset the counter overflowsstatus = sleep;//Go to "sleep"}}else if (status == sleep)//If the state DREAM{if(p++ >= 500) p = 0;//Increment counter for Tweeters not go beyondif(p == 4) PI_PIP;//Pipikaem four times every ~ 15 secondsif(p == 8) PI_PIP;if(p == 12) PI_PIP;if(p == 16) PI_PIP;if(l++ >= 60) l = 0;//Increase the count for the LED does not go beyondif(l == 4) PORT_LED |= (1 << LED);//Morgan twice every 2 secif(l == 8) PORT_LED &= ~(1 << LED);if(l == 12) PORT_LED |= (1 << LED);if(l == 16) PORT_LED &= ~(1 << LED);}}ISR (TIMER1_OVF_vect) //Timer 1 - for maintenance Shimano (overflow ~ 65 ms){if (Temperature < EncData) //If the actual temperature is less than the encoder{if(pwm++ > 1023) pwm = 1023;//Increase the heat, do not go beyond the boundaries of the PWM}if (Temperature > EncData) //If the actual temperature exceeds the set encoder{if(pwm-- <= 0) pwm = 0; //Reduce the heat, do not go beyond the boundaries of the PWM}if (Temperature == EncData) //If the actual temperature is set encoder{pwm = OCR1A; //We leave everything as it was}OCR1A = pwm; //Write a heating value in the comparison register}ISR (TIMER2_OVF_vect) //Timer 2 - for Tweeters{static unsigned char w;//Variable defining the duration of the "pi-beeping"if(w++ < 100) PORT_BUZZER ^= (1 << BUZZER);//If the duration is not over - change the state of the output to the opposite (square wave)else //If the duration is over{w=0;//Counter reset durationTCCR2B = 0;//Timer stop}}int main (void){DDRD = 0xFF;//outputs for displayDDRB = 0xFF;//DDR_BUZZER |= (1 << BUZZER);//Output for TweetersPORT_BUZZER &= ~(1 << BUZZER);//with zeroDDR_LED |= (1 << LED);//Output for LEDsPORT_LED |= (1 << LED);//Highlighting to indicate power-onDDR_BUTTON &= ~(1 << BUTTON);//Login buttonPORT_BUTTON |= (1 << BUTTON);//a pull-up resistorDDR_ENC &= ~(1 << ENC_0);DDR_ENC &= ~(1 << ENC_1);//Inputs for encoderPORT_ENC |= (1 << ENC_0) | (1 << ENC_1);//a pull-up resistorTIMSK0 |= (1 << TOIE0);//Interrupt Enable timer 0 overflowTCCR1A |= (1 << WGM10) | (1 << WGM11) | (1 << COM1A1);//10-bit Phase correct PWM, the output is connected OC1A (neinventirovanny PWM)TIMSK1 |= (1 << TOIE1);//Interrupt Enable Timer overflow 1TCCR1B |= (1 << CS11);// | (1 << CS10);//Start Timer 1 prescaler 64TIMSK2 |= (1 << TOIE2);//Interrupt Enable Timer overflow 2ADMUX |= (1 << REFS0) | (1 << MUX2) | (1 << MUX0);//Reference voltage - supply voltage, 5 th ADC channelADCSRA |= (1 << ADEN) | (1 << ADSC) | (1 << ADPS2) | (1 << ADPS1);EncData = eeprom_read_word (0x10);status = work;sei();for (;;){if(status == work) EncoderScan();A_D_C ();sot = ((*a)%1000)/100;des = (((*a)%1000)%100)/10;edi = (((*a)%1000)%100)%10;podsvet();}}here is the error logCode:Compiling C: Soldier_exm.cavr-gcc -c -mmcu=atmega8 -I. -gdwarf-2 -DF_CPU=8000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=./Soldier_exm.lst -std=gnu99 -Wundef -MMD -MP -MF .dep/Soldier_exm.o.d Soldier_exm.c -o Soldier_exm.oSoldier_exm.c: In function 'EncoderScan':Soldier_exm.c:104: warning: assignment discards qualifiers from pointer target typeSoldier_exm.c:106: error: 'TCCR0B' undeclared (first use in this function)Soldier_exm.c:106: error: (Each undeclared identifier is reported only onceSoldier_exm.c:106: error: for each function it appears in.)Soldier_exm.c:108: error: 'TCCR2B' undeclared (first use in this function)Soldier_exm.c: In function '__vector_9':Soldier_exm.c:141: warning: passing argument 1 of '__eewr_word_m8' makes pointer from integer without a castSoldier_exm.c:142: error: 'TCCR2B' undeclared (first use in this function)Soldier_exm.c:147: warning: assignment discards qualifiers from pointer target typeSoldier_exm.c: In function '__vector_4':Soldier_exm.c:200: error: 'TCCR2B' undeclared (first use in this function)Soldier_exm.c: In function 'main':Soldier_exm.c:217: error: 'TIMSK0' undeclared (first use in this function)Soldier_exm.c:220: error: 'TIMSK1' undeclared (first use in this function)Soldier_exm.c:223: error: 'TIMSK2' undeclared (first use in this function)Soldier_exm.c:227: warning: passing argument 1 of '__eerd_word_m8' makes pointer from integer without a castmake.exe: *** [Soldier_exm.o] Error 1> Process Exit Code: 2> Time Taken: 00:11any help would be appreciated. Have already etched the pcb and populated it with other components.

author: HwAoRrDk
author_rank: Super Contributor
timestamp: February 04, 2017, 12:11:23 am
content: I suppose for a start you could try changing all the erroneous references to registers that don't exist on the ATmega8 to their proper names, and then see if it compiles without error. So, that is:TCCR0B => TCCR0TCCR2B => TCCR2TIMSK0 / TIMSK1 / TIMSK2 => TIMSKThe ATmega8 only has a single timer control register for timers 0 and 2, as well as only a single timer interrupt mask register for all timers, rather than one for each.