The Interfacing Relay with AVR program is very simple and straight forward, which control the relays in AVR Slicker Board. The relay is working that uses a delay procedure loop based software delay. The C programs are developed in CodeVisionAVR software.
C Program to control Relay in AVR
Title : Program to control Relay
#define TXIF UCSRA.5
#define RCIF UCSRA.7
#define FOSC 16000000L //Oscillator Frequency 16Mhz
#define USART_Baud 9600
#define BaudH ((unsigned int)(((FOSC/16/USART_Baud)-1) >> 8))
#define BaudL ((unsigned int)(((FOSC/16/USART_Baud)-1) & 0xFF))
#define Buzzer PORTB.1
#define Relay PORTB.0
void putch(unsigned char);
unsigned char Receive(void);
unsigned char RecDat=0;
DDRB = 0xff;
RecDat = Receive();
case 'C':Relay =1;break;
case 'D':Relay =0;break;
UCSRA = 0x00 //Status Register
UCSRB = 0x18; //Transmit Enable,Receive enable,interrupts disabled
UCSRC = 0x86; //8 databits,no parity,single stop
UBRRH = BaudH;
UBRRL = BaudL;
printf("Buzzer:\tPress 'A' to ON\n\r\tPress 'B' to OFF\n\r");
printf("Relay :\tPress 'C' to ON\n\r\tPress 'D' to OFF\n\r");
void putch(unsigned char Data)
UDR = Data;
unsigned char Receive()
To compile the above C code you need the CodeVision AVR software. The software has it’s own IDE and built-in AVR gcc- Compiler. They must be properly installed and a project with correct settings must be created in order to compile the code. To compile the above code, the C file must be added to the project.
In CodeVision AVR software, you can develop or debug the project without any hardware setup. You must compile the code for generating HEX file. In debugging Mode, you want to check the port output without microcontroller Board.
The AVR Code Blaster software is used to download the hex file into your microcontroller through Parallel port.