I'm trying to compile C program for avr and overtime i Got a strange compiling error. This program wrote for atmega8 micro-controller using Ateml studio
expected ';' before numeric constant
This is my program
/*
* LDR_ADC_ATM8.c
*
* Created: 11/16/2015 9:25:39 AM
* Author: Semira
*/
#include <avr/io.h>
#include <util/delay.h>
// Standard Input/Output functions
#include <stdio.h>
//////////////////////////////////
//Programmer Setting
#define relay PORTB.1
#define ON 1
#define OFF 0
////////////////////////////////
//User Setting
#define Dark 800
#define Light 600
/////////////////////////////////
#define ADC_VREF_TYPE 0x00
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
// Declare your global variables here
unsigned int adc=0;
int main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=0
PORTB=0x00;
DDRB=0x02;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
// ADC initialization
// ADC Clock frequency: 62.500 kHz
// ADC Voltage Reference: AREF pin
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x87;
delay_ms(1000);
while (1)
{
// Place your code here
adc=read_adc(5);
delay_ms(100);
delay_ms(500);
if(adc>Dark)// if darkness
{
delay_ms(10);
relay=ON;
}
if(adc<Light)// if day light
{
delay_ms(10);
relay=OFF;
}
}
}
How can I fix this error ?
Atmel Studio uses AVRGCC, so PORTB.1
is not a valid syntax. To toggle a specific bit on a port:
PORTB |= (1 << PB1); // set bit 1 on PORTB
PORTB &= ~(1 << PB1); // clear bit 1 on PORTB
YOu could use the _BV
macro that is a avr-libc definition and performs a left shift.
#DEFINE _BV(bit) (1 << bit)
So you could also write
PORTB |= _BV(PB1);
To simplify things you could define helper macros, which is pretty common:
#define SETBIT(reg, bit) reg |= 1 << bit
#define CLRBIT(reg, bit) reg &= ~(1 << bit)
Personally, I would probably write a function:
void SetRelay(bool on) {
if (on) {
PORTB |= (1 << PB1);
} else {
PORTB &= ~(1 << PB1);
}
}