Author Topic: Port code for ATMega328 to ATMega8 (timer / counter)  (Read 2600 times)

0 Members and 1 Guest are viewing this topic.

Offline daemon123Topic starter

  • Contributor
  • Posts: 16
  • Country: pk
Port code for ATMega328 to ATMega8 (timer / counter)
« on: February 03, 2017, 02:20:16 pm »
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: [Select]
// Soldering station "JAY-936"
// Author - Chernyakov Sergey S. - [url=http://www.embed.com.ua]www.embed.com.ua[/url]

// 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 figures
unsigned 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 category

q++;
switch(q) //Bust discharge Display Now
{
        case 1:PORT_DIG4 &= ~(1 << DIG4);//Gasim Previous rank
       PORTD &= 0x80; PORTD |= cifra[edi];//Set the corresponding number
               PORT_DIG1 |= (1 << DIG1);//Highlights by following discharge
break;

case 2:PORT_DIG1 &= ~(1 << DIG1);
       PORTD &= 0x80; PORTD |= cifra[des];
               PORT_DIG2 |= (1 << DIG2);
    break;

case 3:PORT_DIG2 &= ~(1 << DIG2);
       PORTD &= 0x80; PORTD |= cifra[sot];
               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 [tis];
               PORT_DIG4 |= (1 << DIG4);
   q=0;//As highlighted in the last 4-th bit counter reset
break;

    default:
    break;
}
}


void EncoderScan(void)//encoder processing function
{
    unsigned char New;//Partly new value encoder
 
    New = PIN_ENC & ((1<< ENC_0) | (1 << ENC_1));// Read the current position of the encoder
 
    if(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 increase
       if(New == 0) EncData--;//Or reduce 
       break;
    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 top
if(EncData < 200) EncData = 200;//and lower limit
        EncState = New; // We write the new value of the previous state
a = &EncData;//Pointer to assign an address encoder current value (set temperature)

TCCR0B = TCNT0 = time = 0;  //Timer 0 is stopped and reset
TCCR0B |= (1 << CS02) | (1 << CS00);//And restart for reference 3 seconds and 15 minutes
PI_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 ADC
    while(!(ADCSRA & (1<<ADIF))); // Wait for the end of conversion
    ADCSRA |=(1<<ADIF);            // Reset completion flag conversion

Usum += 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 temperature
Usum=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 mode

if (status == work)//If the status of the work
{
    time++;//Increase the counter overflows
    if(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 EEPROM
    PI_PIP;//and pipiknut
    }
    }

    if(time >= 91)           //If it has been 3 seconds = 32 ms x 91
    a = &Temperature;    //Pointer assign the address of the current tip temperature

    if(time >= 28125) //If it's been 15 minutes
    {
        TCNT0 = 0;  //Timer 0 reset
    TCCR1B = TCCR1A = 0;//PWM stop
    time = 0;//Reset the counter overflows
        status = sleep;//Go to "sleep"
    }   
}

else if (status == sleep)//If the state DREAM
{
    if(p++ >= 500) p = 0;//Increment counter for Tweeters not go beyond
if(p == 4) PI_PIP;//Pipikaem four times every ~ 15 seconds
    if(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 beyond
if(l == 4) PORT_LED |= (1 << LED);//Morgan twice every 2 sec
if(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 duration
TCCR2B = 0;//Timer stop
}
}

int main (void)
{
DDRD = 0xFF;//outputs for display
DDRB = 0xFF;//
DDR_BUZZER |= (1 << BUZZER);//Output for Tweeters
PORT_BUZZER &= ~(1 << BUZZER);//with zero
DDR_LED |= (1 << LED);//Output for LEDs
PORT_LED |= (1 << LED);//Highlighting to indicate power-on
DDR_BUTTON &= ~(1 << BUTTON);//Login button
PORT_BUTTON |= (1 << BUTTON);//a pull-up resistor
    DDR_ENC &= ~(1 << ENC_0);DDR_ENC &= ~(1 << ENC_1); //Inputs for encoder
PORT_ENC |= (1 << ENC_0) | (1 << ENC_1);//a pull-up resistor

TIMSK0 |= (1 << TOIE0);//Interrupt Enable timer 0 overflow

TCCR1A |= (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 1
TCCR1B |= (1 << CS11);// | (1 << CS10);//Start Timer 1 prescaler 64

TIMSK2 |= (1 << TOIE2);//Interrupt Enable Timer overflow 2

ADMUX |= (1 << REFS0) | (1 << MUX2) | (1 << MUX0);//Reference voltage - supply voltage, 5 th ADC channel
ADCSRA |= (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 log

Code: [Select]
Compiling C: Soldier_exm.c
avr-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.o
Soldier_exm.c: In function 'EncoderScan':
Soldier_exm.c:104: warning: assignment discards qualifiers from pointer target type
[b]Soldier_exm.c:106: error: 'TCCR0B' undeclared (first use in this function)[/b]
Soldier_exm.c:106: error: (Each undeclared identifier is reported only once
Soldier_exm.c:106: error: for each function it appears in.)
[b]Soldier_exm.c:108: error: 'TCCR2B' undeclared (first use in this function)[/b]
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 cast
[b]Soldier_exm.c:142: error: 'TCCR2B' undeclared (first use in this function)[/b]
Soldier_exm.c:147: warning: assignment discards qualifiers from pointer target type
Soldier_exm.c: In function '__vector_4':
[b]Soldier_exm.c:200: error: 'TCCR2B' undeclared (first use in this function)[/b]
Soldier_exm.c: In function 'main':
[b]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)[/b]
Soldier_exm.c:227: warning: passing argument 1 of '__eerd_word_m8' makes pointer from integer without a cast
make.exe: *** [Soldier_exm.o] Error 1

> Process Exit Code: 2
> Time Taken: 00:11


any help would be appreciated. Have already etched the pcb and populated it with other components.
 

Offline HwAoRrDk

  • Super Contributor
  • ***
  • Posts: 1476
  • Country: gb
Re: Port code for ATMega328 to ATMega8 (timer / counter)
« Reply #1 on: February 04, 2017, 12:11:23 am »
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 => TCCR0
TCCR2B => TCCR2
TIMSK0 / TIMSK1 / TIMSK2 => TIMSK

The 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.
 
The following users thanked this post: daemon123


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf