AVRDEM 2 PLUS SAMPLE CODE © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0 AVRDEM 2 PLUS SAMPLE CODE Sample 1. Light LEDs Associated With PORTA................................................. 2 Sample 2. Make the Buzzer Beep ........................................................................ 3 Sample 3. Read Temperature from Built-in TWI Temperature Sensor............. 4 Sample 4. Transmit Data through the USART of ATMEGA16 ........................... 7 Sample 5. How to Display Information on 7-segment LEDs ............................. 9 Sample 6. How to Display Information on HD44780 LCD Module ...................11 Sample 7. LED Mode Code................................................................................. 22 Sample 8. Display Information on the LCD Module. ........................................ 33 © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page 1 AVRDEM 2 PLUS SAMPLE CODE Sample 1. Illuminate LEDs Associated With PORTA This Sample will show you how to illuminate LEDs associated with PORTA of ATMEGA16 microcontroller. LEDs are connected with PORTA1-3. When those pins are set to low, corresponding LED will be illuminated. #include <avr/io.h> #include <avr/delay.h> #include <compat/deprecated.h> #define en_led3 cbi(PORTA,PA0) #define disen_led3 sbi(PORTA,PA0) #define en_led4 cbi(PORTA,PA1) #define disen_led4 sbi(PORTA,PA1) #define en_led5 cbi(PORTA,PA2) #define disen_led5 sbi(PORTA,PA2) #define en_led6 cbi(PORTA,PA3) #define disen_led6 sbi(PORTA,PA3) int main( void ) { DDRA=0xFF; while(1) { en_led3; en_led4; en_led5; en_led6; _delay_ms(8000); _delay_ms(8000); // All PORTA is output port disen_led3; disen_led4; disen_led5; disen_led6; _delay_ms(8000); _delay_ms(8000); } } DB-DP115_Ver1.0 _Page2 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE Sample 2. Make the Buzzer Beep In this Sample, we will show how to make the speaker that connected to PORTD7 (OC2) pin buzz. Before starting this test, you should first connect a passive speaker to J2. The speaker is connected to the collector of a NPN transistor built in ULN2003 chip. Base of the ULN2003 chip is driven by PORTD7 (OC2 Pin). When a PWM wave is applied on the PORTD7 pin, the speaker will start buzzing. #include #include #include #include #include <stdint.h> <avr/io.h> <util/twi.h> <avr/delay.h> <compat/deprecated.h> #define uchar unsigned char #define uint unsigned int #define FREQ 4 void DelayMs(uint ms) { uint i; for(i=0;i<ms;i++) _delay_loop_2(FREQ*250); } void PWM(uchar pwmduty) { TCCR2=_BV(WGM20)|_BV(COM21)|_BV(CS22); //CLK/64 TCNT2=0; DDRD |= _BV(PD7); OCR2=pwmduty; DelayMs(100); } int main (void) { uint i=0; while(1) © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page3 AVRDEM 2 PLUS SAMPLE CODE { i++; if(i%100==0) PWM(4); else PWM(0); } } Sample 3. Read Temperature from Built-in TWI Temperature Sensor This Sample will show how to read temperature value from LM75A temperature sensor via TWI interface of ATMEGA16 control microchip. In this demo code, only IIC operation of ATMEGA16 control microchip is displayed. LM75A is connected to PORTC0 and PORTC1. When correct time sequence has been applied on those 2 pins, temperature data can be obtained from LM75A temperature sensor. #include <stdint.h> #include <avr/io.h> #include <util/twi.h> #include <avr/delay.h> #include <compat/deprecated.h> #define uchar unsigned char #define uint unsigned int #define FREQ 4 #define TempRegAddr 0x00 // Address of Temperature sensor #define ReadLM75Addr 0x91 // Device address is 0. Read #define WriteLM75Addr 0x90 // Write #define Twi_Stop() TWCR=_BV(TWINT)|_BV(TWSTO)|_BV(TWEN) #define Twi_Start() TWCR=_BV(TWINT)|_BV(TWSTA)|_BV(TWEN) #define check_TWINT() while(!(TWCR & (1<<TWINT))) // Wait for TWINT flag being set uint cvalue,fvalue; char cent_buf[6],fahr_buf[6]; DB-DP115_Ver1.0 _Page4 // Memory Centigrade and Fahrenheit value // Centigrade and Fahrenheit value array © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE uchar TempHigh; uchar TempLow; uchar TempSign,TempData; uchar TwiStatus; void twi_init(void) { TWCR= 0X00; //disable twi TWBR= 0x12; //set bit rate TWSR= 0x01; //set prescale TWCR= 0x04; //enable twi } void TWITempRead(uchar ReadDeviceAddr,uchar WriteDeviceAddr,uchar RegAddr) { uint j; DelayMs(20); while (TwiStatus != 0x08) // Check TWSR register. Clear the prescale bit. { Twi_Start(); // Send START signal _delay_us(1); TwiStatus=TWSR & 0xF8; } //StatusLihgting(0); while (TwiStatus != 0x18) // Check TWSR register. SLA+W signal has been //sent, and ACK signal has been received. { TWDR = WriteDeviceAddr; // Device Address (write) TWCR=0x84; _delay_us(1); TwiStatus=TWSR & 0xF8; } while (TwiStatus != 0x28) //DATA has been sent. ACK has been received { TWDR = RegAddr; // Write LM75 address. TWCR=0x84; _delay_us(1); TwiStatus=TWSR & 0xF8; } © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page5 AVRDEM 2 PLUS SAMPLE CODE while (TwiStatus != 0x10) //REPEATED START has been sent. { Twi_Start(); // Send REPEATED START _delay_us(1); TwiStatus=TWSR & 0xF8; } while (TwiStatus != 0x40) // Check TWSR register. SLA+R has been sent. //ACK has been received. { TWDR =ReadDeviceAddr; TWCR=0x84; _delay_us(1); TwiStatus=TWSR & 0xF8; // Device address } TwiStatus=0x00; while (TwiStatus != 0x50) // DATA has been received. ACK has been sent. { TWCR=0xc4; // Receive high 8-bit data. ACK has been sent. _delay_us(20); TempHigh=TWDR; TwiStatus=TWSR & 0xF8; } while(TwiStatus != 0x58) //DATA has been received. NACK has been sent. { TwiStatus=TWSR & 0xF8; TWCR=0x84; // Receive low 8-bit data. nACK has been sent. _delay_us(10); TempLow=TWDR; } Twi_Stop(); // Send STOP } void LM75_Temperature(void) { unsigned int temp_H,temp_L; TWITempRead(ReadLM75Addr,WriteLM75Addr,TempRegAddr); temp_H=TempHigh; //High bits DB-DP115_Ver1.0 _Page6 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE temp_L=TempLow; //Compute Centigrade cvalue=(temp_H<<8)|temp_L; cent_buf[0]=' '; if((cvalue&0x80)==1) { cvalue=~cvalue+1; cent_buf[0]='-'; } cvalue=cvalue>>5; cvalue=cvalue * 1.25; cent_buf[1]=cvalue/100+48; cent_buf[2]=(cvalue/10)%10+48; cent_buf[3]='.'; cent_buf[4]=cvalue%10+48; cent_buf[5]='\0'; //compute Fahrenheit fvalue=((cvalue*9)/5)+32; fahr_buf[0]=' '; if((fvalue&0x80)==1) { fvalue=~fvalue+1; fahr_buf[0]='-'; } fahr_buf[1]=fvalue/100+48; fahr_buf[2]=(fvalue/10)%10+48; fahr_buf[3]='.'; fahr_buf[4]=fvalue%10+48; fahr_buf[5]='\0'; //Low bits //Calculate Base Complement //Calculate Base Complement } Sample 4. Transmit Data through the USART of ATMEGA16 This Sample will show you how to transmit data through USART of ATMEGA16 microcontroller. User must configure Hyper Terminal or other terminal software in PC © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page7 AVRDEM 2 PLUS SAMPLE CODE correctly, and then connect this development board to PC. A single-chip USB to UART Bridge CP2102 is connected to PORTD0/RXD, PORTD1/ TXD of ATMEGA16 microcontroller. #include <stdint.h> #include <avr/io.h> #include <avr/delay.h> #define uchar unsigned char #define uint unsigned int uchar testuart[]={0x30,0x31,0x32,0x33}; void InitUsart(void) //USART initializtion { DDRD=0xFE; //PD0/RXD input, PD1/TXD output. UCSRB=0x00; UCSRA=0x00; UBRRH=0x00; UBRRL=0x0c; // USART Baud Rate is 57600bps UCSRC=((1<<URSEL)|(3<<UCSZ0))&(~_BV(USBS)); //Set frame format: 8data, 1stop bit UCSRB=_BV(RXEN)|_BV(TXEN); //Enable receiver and transmitter } void USART(uchar *sendbuf) { while((*sendbuf)!=0) { UDR = *(sendbuf++); while(!(UCSRA & 0x40)) ; UCSRA |= 0x40; } } int main (void) { InitUsart(); while(1) { USART(testuart); DB-DP115_Ver1.0 _Page8 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE _delay_ms(2000); } } Sample 5. How to Display Information on 7-segment LEDs This demonstration board shows how to display information on 7-segment LEDs through GPIOs of ATMEGA16 microchip. PORTB is connected to a-h pins of 7segment LEDs, and PORTA4-7 are connected to ULN2003 chip, that drive 4 cathodes of those LEDs. Remove the LCD panel above the LED segments. Transmit data via PORTB and enable one of the cathodes, then the LED segments will display a digit letter. If switch of cathode is quick enough, it will display 4 digits and looks seamlessly. #include <stdint.h> #include <avr/io.h> #include <avr/delay.h> #include <compat/deprecated.h> //Set LED common port #define set_ledcom_port0 sbi(PORTA,PA7) #define clr_ledcom_port0 cbi(PORTA,PA7) #define set_ledcom_port1 sbi(PORTA,PA6) #define clr_ledcom_port1 cbi(PORTA,PA6) #define set_ledcom_port2 sbi(PORTA,PA5) #define clr_ledcom_port2 cbi(PORTA,PA5) #define set_ledcom_port3 sbi(PORTA,PA4) #define clr_ledcom_port3 cbi(PORTA,PA4) //Set LED data port #define set_leddata0 sbi(PORTB,PB0) #define set_leddata1 sbi(PORTB,PB1) #define set_leddata2 sbi(PORTB,PB2 #define set_leddata3 sbi(PORTB,PB3) #define set_leddata4 sbi(PORTB,PB4) #define set_leddata5 sbi(PORTB,PB5) #define set_leddata6 sbi(PORTB,PB6) #define set_leddata7 sbi(PORTB,PB7) © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page9 AVRDEM 2 PLUS SAMPLE CODE #define clr_leddata0 #define clr_leddata1 #define clr_leddata2 #define clr_leddata3 #define clr_leddata4 #define clr_leddata5 #define clr_leddata6 #define clr_leddata7 cbi(PORTB,PB0) cbi(PORTB,PB1) cbi(PORTB,PB2 cbi(PORTB,PB3) cbi(PORTB,PB4) cbi(PORTB,PB5) cbi(PORTB,PB6) cbi(PORTB,PB7) // Main Programmer void main( void ) { DDRB |= 0xf0; // Make common port ouput DDRB=0xff; // Make data port ouput while(1) { Clr_ledcom_port0; Clr_ledcom_port1; Clr_ledcom_port2; set_ledcom_port 3; CLR_LEDDATA0; SET_LEDDATA1; SET_LEDDATA2; CLR_LEDDATA3; CLR_LEDDATA4; CLR_LEDDATA5; CLR_LEDDATA6; CLR_LEDDATA7; Delay100TCYx(2); Clr_ledcom_port0; Clr_ledcom_port1; set_ledcom_port 2; Clr_ledcom_port3; SET_LEDDATA0; SET_LEDDATA1; CLR_LEDDATA2; SET_LEDDATA3; SET_LEDDATA4; CLR_LEDDATA5; SET_LEDDATA6; CLR_LEDDATA7; Delay100TCYx(2); Clr_ledcom_port0; set_ledcom_port 1; Clr_ledcom_port2; DB-DP115_Ver1.0 _Page10 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE Clr_ledcom_port3; SET_LEDDATA0; SET_LEDDATA1; SET_LEDDATA2; SET_LEDDATA3; CLR_LEDDATA4; CLR_LEDDATA5; SET_LEDDATA6; CLR_LEDDATA7; Delay100TCYx(2); set_ledcom_port 0; Clr_ledcom_port1; Clr_ledcom_port2; Clr_ledcom_port3; CLR_LEDDATA0; SET_LEDDATA1; SET_LEDDATA2; CLR_LEDDATA3; CLR_LEDDATA4; SET_LEDDATA5; SET_LEDDATA6; CLR_LEDDATA7; Delay100TCYx(2); } } Sample 6. How to Display Information on HD44780 LCD Module This Sample will show you how to display information on HD44780 compatible LCD Module. The LCD module is connected to PORTB (as data port), PORTD4 (E signal of LCD module), PORTD3 (RW signal of LCD module), and PORTD2 (RS signal of LCD module). Before using this function, you should install the LCD panel with screws. Adjust R28 to change the contrast of the LCD panel. #include <stdint.h> #include <avr/io.h> #include <util/twi.h> #include <avr/delay.h> #include <compat/deprecated.h> #include <avr/interrupt.h> #include <avr/hd44780.h> #define uchar unsigned char © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page11 AVRDEM 2 PLUS SAMPLE CODE #define uint unsigned int char LCD_name[]="Sure Electronics"; char LCD_Ver[]="Ver1.0"; void InitPort(void) { DDRB=0xFF; DDRD=0xFE; } // All PORTB is output port /************* LCD ************************************ Beginning *******************************/ /* DATA_PORT defines the port to which the LCD data lines are connected */ #define DATA_PORT PORTB #define DATA_PIN PINB #define TRIS_DATA_PORT DDRB /* CTRL_PORT defines the port where the control lines are connected. * These are just samples, change to match your application. */ #define SET_RW_PIN sbi(PORTD,PD3) /* PORT for RW, RW=1 */ #define CLR_RW_PIN cbi(PORTD,PD3) /* RW=0 */ #define SET_TRIS_RW sbi(DDRD,DDD3) /* TRIS for RW */ #define CLR_TRIS_RW cbi(DDRD,DDD3) /* TRIS for RW */ #define SET_RS_PIN sbi(PORTD,PD2) /* PORT for RS, RS=1 */ #define CLR_RS_PIN cbi(PORTD,PD2) /* PORT for RS, RS=0 */ #define SET_TRIS_RS sbi(DDRD,DDD2) /* TRIS for RS */ #define CLR_TRIS_RS cbi(DDRD,DDD2) /* TRIS for RS */ #define SET_E_PIN sbi(PORTD,PD4) /* PORT for E ,1 */ #define CLR_E_PIN cbi(PORTD,PD4) /* PORT for E ,0 */ #define SET_TRIS_E sbi(DDRD,DDD4) /* TRIS for E */ #define CLR_TRIS_E cbi(DDRD,DDD4) /* TRIS for E */ /* Display ON/OFF Control defines */ #define DON 0b00001111 /* Display on */ #define DOFF 0b00001011 /* Display off */ #define CURSOR_ON 0b00001111 /* Cursor on */ #define CURSOR_OFF 0b00001101 /* Cursor off */ #define BLINK_ON 0b00001111 /* Cursor Blink */ #define BLINK_OFF 0b00001110 /* Cursor No Blink */ /* Cursor or Display Shift defines */ #define SHIFT_CUR_LEFT 0b00010011 #define SHIFT_CUR_RIGHT 0b00010111 #define SHIFT_DISP_LEFT 0b00011011 #define SHIFT_DISP_RIGHT 0b00011111 /* Cursor shifts to the left */ /* Cursor shifts to the right */ /* Display shifts to the left */ /* Display shifts to the right */ /* Function Set defines */ DB-DP115_Ver1.0 _Page12 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE #define EIGHT_BIT 0b00111111 /* 8-bit Interface */ #define LINES_5X7 0b00111011 /* 5x7 characters, multiple line */ #define PARAM_SCLASS auto #define MEM_MODEL far /* Change this to near for small memory model */ /* OpenXLCD * Configures I/O pins for external LCD */ void OpenXLCD(uchar); /* SetCGRamAddr * Sets the character generator address */ void SetCGRamAddr(uchar); /* SetDDRamAddr * Sets the display data address */ void SetDDRamAddr(uchar); /* BusyXLCD * Returns the busy status of the LCD */ uchar BusyXLCD(void); /* ReadAddrXLCD * Reads the current address */ uchar ReadAddrXLCD(void); /* ReadDataXLCD * Reads a byte of data */ char ReadDataXLCD(void); /* WriteCmdXLCD * Writes a command to the LCD */ void WriteCmdXLCD(uchar); /* WriteDataXLCD * Writes a data byte to the LCD */ void WriteDataXLCD(char); /* putcXLCD * A putc is a write */ © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page13 AVRDEM 2 PLUS SAMPLE CODE #define putcXLCD WriteDataXLCD /* putsXLCD * Writes a string of characters to the LCD */ void putsXLCD(char *); /* putrsXLCD * Writes a string of characters in ROM to the LCD */ void putsXLCD(char *buffer); /******************************************************************** * Function Name: BusyXLCD * * Return Value: char: busy status of LCD controller * * Parameters: void * * Description: This routine reads the busy status of the * * Hitachi HD44780 LCD controller. * ********************************************************************/ uchar BusyXLCD(void) { SET_RW_PIN; // Set the control bits for read BF CLR_RS_PIN; _delay_us(10); SET_E_PIN; // Clock in the command _delay_us(10); if(DATA_PIN&0x80) // Read bit 7 (busy bit) { // If high CLR_E_PIN; // Reset clock line CLR_RW_PIN; // Reset control line return 1; // Return TRUE } else // Bit 7 low { CLR_E_PIN; // Reset clock line CLR_RW_PIN; // Reset control line return 0; // Return FALSE } } /******************************************************************** * Function Name: OpenXLCD * Return Value: void * * Parameters: lcdtype: sets the type of LCD (lines) * * Description: This routine configures the LCD. Based on * * the Hitachi HD44780 LCD controller. The * * routine will configure the I/O pins of the * * microcontroller, setup the LCD for 4- or * DB-DP115_Ver1.0 _Page14 * © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE * 8-bit mode and clear the display. The user * * must provide three delay routines: * * DelayFor18TCY() provides a 18 Tcy delay * * DelayPORXLCD() provides at least 15ms delay * * DelayXLCD() provides at least 5ms delay * ********************************************************************/ void OpenXLCD(uchar lcdtype) { // The data bits are made into inputs DATA_PIN = 0; TRIS_DATA_PORT = 0x00; SET_TRIS_RW; SET_TRIS_RS; SET_TRIS_E; CLR_RW_PIN; CLR_RS_PIN; CLR_E_PIN; // All control signals made outputs // R/W pin made low // Register select pin made low // Clock pin made low // Delay for more than 15ms to allow for LCD Power on reset _delay_ms(16); // Setup interface to LCD DATA_PORT = 0; TRIS_DATA_PORT = 0xff; DATA_PORT = 0b00110000; CLR_RW_PIN; CLR_RS_PIN; SET_E_PIN; _delay_us(10); CLR_E_PIN; // Data port output // Function set cmd(8-bit interface) // R/W pin made low // Register select pin made low // Clock the cmd in // Delay for at least 4.1ms _delay_ms(6); // Setup interface to LCD DATA_PORT = 0; DATA_PORT = 0b00110000; CLR_RW_PIN; CLR_RS_PIN; SET_E_PIN; _delay_us(10); CLR_E_PIN; // Function set cmd(8-bit interface) // R/W pin made low // Register select pin made low // Clock the cmd in // Delay for at least 100us © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page15 AVRDEM 2 PLUS SAMPLE CODE _delay_ms(1); // Setup interface to LCD DATA_PORT = 0; DATA_PORT = 0b00110000; SET_E_PIN; _delay_us(10); CLR_E_PIN; TRIS_DATA_PORT = 0x00; // Function set cmd(8-bit interface) // Clock cmd in // Make data port input // Set data interface width, # lines, font while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(lcdtype); // Function set cmd // Turn the display on then off while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(DOFF); // Display OFF/Blink OFF while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(DON); // Display ON/Blink ON while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(BLINK_ON&CURSOR_ON); // Display ON/Blink ON // Clear display while(BusyXLCD()); WriteCmdXLCD(0x01); // Wait if LCD busy // Clear display // Set entry mode inc, no shift while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(SHIFT_CUR_LEFT); // Entry Mode while(BusyXLCD()); WriteCmdXLCD(0b00000110); // Set DD Ram address to 0 while(BusyXLCD()); SetDDRamAddr(0b00000111); // Wait if LCD busy // Set Display data ram address to 0 return; } /******************************************************************** * Function Name: putrsXLCD * Return Value: void DB-DP115_Ver1.0 _Page16 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE * Parameters: buffer: pointer to string * Description: This routine writes a string of bytes to the * Hitachi HD44780 LCD controller. The user * must check to see if the LCD controller is * busy before calling this routine. The data * is written to the character generator RAM or * the display data RAM depending on what the * previous SetxxRamAddr routine was called. ********************************************************************/ /* void putrsXLCD(const rom char *buffer) { while(*buffer) // Write data to LCD up to null { while(BusyXLCD()); // Wait while LCD is busy WriteDataXLCD(*buffer); // Write character to LCD buffer++; // Increment buffer } return; } */ /******************************************************************** * Function Name: putsXLCD * Return Value: void * Parameters: buffer: pointer to string * Description: This routine writes a string of bytes to the * Hitachi HD44780 LCD controller. The user * must check to see if the LCD controller is * busy before calling this routine. The data * is written to the character generator RAM or * the display data RAM depending on what the * previous SetxxRamAddr routine was called. ********************************************************************/ void putsXLCD(char *buffer) { while(*buffer) // Write data to LCD up to null { while(BusyXLCD()) ; // Wait while LCD is busy WriteDataXLCD(*buffer); // Write character to LCD buffer++; // Increment buffer } return; } /********************************************************************* * Function Name: ReadAddrXLCD * Return Value: char: address from LCD controller © 2004 -2008 Sure Electronics Inc. * * DB-DP115_Ver1.0_Page17 AVRDEM 2 PLUS SAMPLE CODE * Parameters: void * * Description: This routine reads an address byte from the * * Hitachi HD44780 LCD controller. The user * * must check to see if the LCD controller is * * busy before calling this routine. The address* * is read from the character generator RAM or * * the display data RAM depending on what the * * previous SetxxRamAddr routine was called. * *********************************************************************/ uchar ReadAddrXLCD(void) { char data; // Holds the data retrieved from the LCD SET_RW_PIN; CLR_RS_PIN; _delay_us(10); SET_E_PIN; _delay_us(10); data = DATA_PIN; CLR_E_PIN; CLR_RW_PIN; return (data&0x7f); // Set control bits for the read // Clock data out of the LCD controller // Save the data in the register // Reset the control lines // Return the address, Mask off the busy bit } /******************************************************************** * Function Name: ReadDataXLCD * Return Value: char: data byte from LCD controller * * Parameters: void * * Description: This routine reads a data byte from the * * Hitachi HD44780 LCD controller. The user * * must check to see if the LCD controller is * * busy before calling this routine. The data * * is read from the character generator RAM or * * the display data RAM depending on what the * * previous SetxxRamAddr routine was called. * ********************************************************************/ char ReadDataXLCD(void) { char data; SET_RS_PIN; SET_RW_PIN; _delay_us(10); DB-DP115_Ver1.0 _Page18 * // Set the control bits © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE SET_E_PIN; _delay_us(10); data = DATA_PIN; CLR_E_PIN; CLR_RS_PIN; CLR_RW_PIN; CLR_E_PIN; _delay_us(10); SET_E_PIN; _delay_us(10); CLR_E_PIN; CLR_RS_PIN; CLR_RW_PIN; return(data); // Clock the data out of the LCD // Read the data // Reset the control bits // Reset the clock line // Clock the next nibble out of the LCD // Reset the control bits // Return the data byte } /******************************************************************** * Function Name: SetCGRamAddr * * Return Value: void * * Parameters: CGaddr: character generator ram address * * Description: This routine sets the character generator * * address of the Hitachi HD44780 LCD * * controller. The user must check to see if * * the LCD controller is busy before calling * * this routine. * ********************************************************************/ void SetCGRamAddr(uchar CGaddr) { TRIS_DATA_PORT = 0xff; // Make data port ouput DATA_PORT = CGaddr | 0b01000000; // Write cmd and address to port CLR_RW_PIN; // Set control signals CLR_RS_PIN; _delay_us(10); SET_E_PIN; // Clock cmd and address in _delay_us(10); CLR_E_PIN; _delay_us(10); TRIS_DATA_PORT = 0x00; // Make data port inputs return; } /******************************************************************** * Function Name: SetDDRamAddr © 2004 -2008 Sure Electronics Inc. * DB-DP115_Ver1.0_Page19 AVRDEM 2 PLUS SAMPLE CODE * Return Value: void * * Parameters: CGaddr: display data address * * Description: This routine sets the display data address * * of the Hitachi HD44780 LCD controller. The * * user must check to see if the LCD controller* * is busy before calling this routine. * ********************************************************************/ void SetDDRamAddr(uchar DDaddr) { TRIS_DATA_PORT = 0xff; // Make port output DATA_PORT = DDaddr | 0b10000000; // Write cmd and address to port CLR_RW_PIN; // Set the control bits CLR_RS_PIN; _delay_us(10); SET_E_PIN; // Clock the cmd and address in _delay_us(10); CLR_E_PIN; _delay_us(10); TRIS_DATA_PORT = 0; // Make port input return; } /******************************************************************** * Function Name: WriteCmdXLCD * * Return Value: void * * Parameters: cmd: command to send to LCD * * Description: This routine writes a command to the Hitachi* * HD44780 LCD controller. The user must check * * to see if the LCD controller is busy before * * calling this routine. * ********************************************************************/ void WriteCmdXLCD(uchar cmd) { TRIS_DATA_PORT = 0xff; // Data port output DATA_PORT = 0; DATA_PORT |= cmd; // Write command to data port CLR_RW_PIN; // Set the control signals CLR_RS_PIN; // for sending a command _delay_us(10); SET_E_PIN; // Clock the command in _delay_us(10); CLR_E_PIN; _delay_us(10); TRIS_DATA_PORT = 0x00; // Data port input return; } DB-DP115_Ver1.0 _Page20 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE /************************************************************************************ * Function Name: WriteDataXLCD * * Return Value: void * * Parameters: data: data byte to be written to LCD * * Description: This routine writes a data byte to the * * Hitachi HD44780 LCD controller. The user * * must check to see if the LCD controller is * * busy before calling this routine. The data * * is written to the character generator RAM or * * the display data RAM depending on what the * * previous SetxxRamAddr routine was called. * *************************************************************************************/ void WriteDataXLCD(char data) { TRIS_DATA_PORT = 0xff; // Make port output DATA_PORT = data; // Write data to port SET_RS_PIN; // Set control bits CLR_RW_PIN; _delay_us(10); SET_E_PIN; // Clock data into LCD _delay_us(10); CLR_E_PIN; CLR_RS_PIN; // Reset control bits TRIS_DATA_PORT = 0x00; // Make port input return; } /************** LCD ************************************** END *********************************/ int main (void) { uchar i; char display_name[]="Sure Electronics"; char display_Ver[]="Ver 2.1"; InitPort(); InitLCD(); // Configure external LCD OpenXLCD( EIGHT_BIT&LINES_5X7 ); // Write to LCD while(BusyXLCD()); putsXLCD(display_name); // Wait if LCD busy // Write to LCD while(BusyXLCD()); SetDDRamAddr(0x40); // Wait if LCD busy © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page21 AVRDEM 2 PLUS SAMPLE CODE putsXLCD(display_Ver); while(1){}; } Sample 7. LED Mode Code This demonstration shows how to display message on 7segment LEDs through GPIOs of ATMEGA16 microcontroller. The 8 bits data output of PORTB is connected to a-h of 7segment LEDs and PORTA4-7 is connected to base of ULN2003, then drive 4 cathode of those LEDs. Remove the LCD panel on the LED segments. Send data from PORTB and enable one of the cathodes. The LED segments will display a digit. If switch-on and switch-off of the four cathodes can be turned quickly enough, it will display 4 digits just like simultaneously. That looks seamlessly. #include <stdint.h> #include <avr/io.h> #include <util/twi.h> #include <avr/delay.h> #include <compat/deprecated.h> #include <avr/interrupt.h> #define uchar unsigned char #define uint unsigned int #define FREQ 4 #define TempRegAddr 0x00 // Address of Temperature sensor #define ReadLM75Addr 0x91 // Device address is 0. Read #define WriteLM75Addr 0x90 // Write #define Twi_Stop() TWCR=_BV(TWINT)|_BV(TWSTO)|_BV(TWEN) #define Twi_Start() TWCR=_BV(TWINT)|_BV(TWSTA)|_BV(TWEN) #define check_TWINT() while(!(TWCR & (1<<TWINT))) // Wait for TWINT flag being set #define leddata_port PORTB #define set_ledcom_port0 sbi(PORTA,PA7) #define clr_ledcom_port0 cbi(PORTA,PA7) #define set_ledcom_port1 sbi(PORTA,PA6) #define clr_ledcom_port1 cbi(PORTA,PA6) #define set_ledcom_port2 sbi(PORTA,PA5) #define clr_ledcom_port2 cbi(PORTA,PA5) #define set_ledcom_port3 sbi(PORTA,PA4) DB-DP115_Ver1.0 _Page22 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE #define clr_ledcom_port3 cbi(PORTA,PA4) #define en_led3 cbi(PORTA,PA0) #define disen_led3 sbi(PORTA,PA0) #define en_led4 cbi(PORTA,PA1) #define disen_led4 sbi(PORTA,PA1) #define en_led5 cbi(PORTA,PA2) #define disen_led5 sbi(PORTA,PA2) #define en_led6 cbi(PORTA,PA3) #define disen_led6 sbi(PORTA,PA3) #define Func_key (PINC&0x80) #define Change_key (PINC&0x40) // Function key // Switch key uint cvalue,fvalue; // Memory Centigrade and Fahrenheit value char cent_buf[6],fahr_buf[6]; // Centigrade and Fahrenheit value array uchar reset_key_pressed,last_reset_key_pressed; uchar Func_key_pressed,last_Func_key_pressed; uchar change_key_pressed,last_change_key_pressed; uchar LED_thousand,LED_hundred,LED_ten,LED_one; uchar value_thousand,value_hundred,value_ten,value_one; uchar temperature_flag; uchar cnt_fuckey; uchar key_flag=0; char pointer=0; uchar TempHigh; uchar TempLow; uchar TempSign,TempData; uchar TwiStatus; const uchar Digital_TAB[]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f, 0x6d,0x1c,0x50,0x79,0x86,0x06}; // 0123456789 SurE 1.1 char Cent[]="Centigrade"; char Fahr[]="Fahrenheit"; char Current[]="Current Temperatrue"; void InitPort(void); void InitUsart(void); void InitTwi(void); void InitDevice(void); void DelayMs(uint ms); void PWM(uchar pwmduty); char Funckey(void); void ChangeKey(void); void LED_InitData(void); void LED_Data(uint data); © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page23 AVRDEM 2 PLUS SAMPLE CODE void LED_Display(uchar num); void TWITempRead(uchar ReadDeviceAddr,uchar WriteDeviceAddr,uchar RegAddr) ; void LM75_Temperature(void); void USART(char *sendbuf); /*********** Initializtion ************************************* Beginning ***********************/ void InitPort(void) { ACSR &=0x7f; DDRA=0xFF; DDRB=0xFF; DDRC=0x3C; // Close Comparator // All PortA is output port // All PORTB is output port // PC7 is for Switch SW1 Input // PC6 is for Switch SW2 Input //PC0,PC1 is for TWI PORTC |=_BV(PORTC6)|_BV(PORTC7)|_BV(PORTC0)|_BV(PORTC1); SFIOR&=~_BV(PUD); DDRD=0xFE; } void InitUsart(void) { UCSRB=0x00; UCSRA=0x00; UBRRH=0x00; UBRRL=0x0c; // USART Baud Rate is 57600bps UCSRC=((1<<URSEL)|(3<<UCSZ0))&(~_BV(USBS)); //Set frame format: 8data, 1stop bit UCSRB=_BV(RXEN)|_BV(TXEN); //Enable receiver and transmitter } void InitDevice(void) { cli(); InitPort(); InitUsart(); } /*********** Initializtion ************************************* END ***********************/ /*********** PWM ******************************* Beginning ******************************/ void DelayMs(uint ms) { uint i; for(i=0;i<ms;i++) _delay_loop_2(FREQ*250); } DB-DP115_Ver1.0 _Page24 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE void PWM(uchar pwmduty) { TCCR2=_BV(WGM20)|_BV(COM21)|_BV(CS22); TCNT2=0; DDRD |= _BV(PD7); OCR2=pwmduty; DelayMs(100); } //CLK/64 /*********** PWM ******************************* END ******************************/ /*********** Scan key status ******************************* Beginning *******************/ char Funckey(void) { uchar temp; if(Func_key==0x80) Func_key_pressed=1; else if(Func_key==0) Func_key_pressed=0; if((Func_key_pressed==0)&(last_Func_key_pressed==1)) { _delay_ms(1); if(Func_key_pressed==0)cnt_fuckey++; } last_Func_key_pressed=Func_key_pressed; temp=cnt_fuckey%4; return temp; } /*********** Scan key status ******************************* END ******************************/ /*********** LED data and display******************************* Beginning ******************/ void LED_InitData(void) { uchar i,j; uchar temp; while(1) { i++; j=i%4; LED_Display(j); //Display Characters if(i==1) { temp++; if(temp==2) © 2004 -2008 Sure Electronics Inc. //Display "Sure" when Power on or after Resetting DB-DP115_Ver1.0_Page25 AVRDEM 2 PLUS SAMPLE CODE { value_thousand=0x6d; value_hundred=0x1c; value_ten=0x50; value_one=0x79; } if(temp==120) { value_thousand=0x00; value_hundred=0x00; value_ten=0x86; value_one=0x06; } //Then Display "1.1" at some Intervals } if(temp==255)break; } } void LED_Data(uint data) { uint i; LED_thousand=0; LED_hundred=0; LED_ten=0; LED_one=0; for(i=0;i<data;i++) //Data is divided into four digits { LED_one++; if(LED_one>=10){LED_one=0;LED_ten++;} if(LED_ten>=10){LED_ten=0;LED_hundred++;} if(LED_hundred>=10){LED_hundred=0;LED_thousand++;} } for(i=0;i<10;i++) // Compute which digit to display on corresponding position { if(i==LED_thousand){value_thousand=Digital_TAB[i];} if(i==LED_hundred){value_hundred=Digital_TAB[i];} if(i==LED_ten){value_ten=Digital_TAB[i];} if(i==LED_one){value_one=Digital_TAB[i];} } if(temperature_flag==1) { value_ten=value_ten|0x80; // Temperature value should have a decimal point temperature_flag=0; } if((cent_buf[0]=='-')|(fahr_buf[0]=='-')) { value_thousand=0x64; } DB-DP115_Ver1.0 _Page26 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE } void LED_Display(uchar num) { clr_ledcom_port0; clr_ledcom_port1; clr_ledcom_port2; clr_ledcom_port3; switch(num) { case(0): { set_ledcom_port3; leddata_port=value_thousand; }break; case(1): { set_ledcom_port2; leddata_port=value_hundred; }break; case(2): { set_ledcom_port1; leddata_port=value_ten; }break; case(3): { set_ledcom_port0; leddata_port=value_one; }break; } //Display Top Digit //Display Second-order Digit //Display Third-order Digit //Display Least Significant Digit } /*********** LED data and display******************************* END *****************/ /*********** TWI operation routine ********************** Beginning **********************/ void InitTwi(void) { TWCR= 0X00; //disable twi TWBR= 0x12; //set bit rate TWSR= 0x01; //set prescale TWCR= 0x04; //enable twi } void TWITempRead(uchar ReadDeviceAddr,uchar WriteDeviceAddr,uchar RegAddr) { while (TwiStatus != 0x08) // Check TWSR register. Clear the prescale bit. © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page27 AVRDEM 2 PLUS SAMPLE CODE { Twi_Start(); // Send START signal _delay_us(1); TwiStatus=TWSR & 0xF8; } //StatusLihgting(0); while (TwiStatus != 0x18) // Check TWSR register. SLA+W signal has been //sent, and ACK signal has been received. { TWDR = WriteDeviceAddr; TWCR=0x84; _delay_us(1); TwiStatus=TWSR & 0xF8; // Device Address (write) } while (TwiStatus != 0x28) //DATA has been sent. ACK has been received { TWDR = RegAddr; // Write LM75 address. TWCR=0x84; _delay_us(1); TwiStatus=TWSR & 0xF8; } while (TwiStatus != 0x10) //REPEATED START has been sent. { Twi_Start(); // Send REPEATED START _delay_us(1); TwiStatus=TWSR & 0xF8; } while (TwiStatus != 0x40) // Check TWSR register. SLA+R has been sent. //ACK has been received. { TWDR =ReadDeviceAddr; // Device address TWCR=0x84; _delay_us(1); TwiStatus=TWSR & 0xF8; } TwiStatus=0x00; while (TwiStatus != 0x50) // DATA has been received. ACK has been sent. { TWCR=0xc4; // Receive high 8-bit data. ACK has been sent. DB-DP115_Ver1.0 _Page28 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE _delay_us(20); TempHigh=TWDR; TwiStatus=TWSR & 0xF8; } while(TwiStatus != 0x58) //DATA has been received. NACK has been sent. { TwiStatus=TWSR & 0xF8; TWCR=0x84; // Receive low 8-bit data. nACK has been sent. _delay_us(10); TempLow=TWDR; } Twi_Stop(); // Send STOP } /*********** TWI operation routine ******************************** END ****************/ /*********** LM75 Read routine ************************************* Beginning ***********/ void LM75_Temperature(void) //Read temperature value by LM75 temperature sensor { uint temp_H,temp_L; TWITempRead(ReadLM75Addr,WriteLM75Addr,TempRegAddr); temp_H=TempHigh; temp_L=TempLow; //Compute Centigrade cvalue=(temp_H<<8)|temp_L; cent_buf[0]=' '; if((cvalue&0x80)==1) { cvalue=~cvalue+1; cent_buf[0]='-'; } cvalue=cvalue>>5; cvalue=cvalue * 1.25; cent_buf[1]=cvalue/100+48; cent_buf[2]=(cvalue/10)%10+48; cent_buf[3]='.'; cent_buf[4]=cvalue%10+48; cent_buf[5]='\0'; //High bits //Low bits //Calculate Base Complement //compute Fahrenheit fvalue=((cvalue*9)/5)+32; fahr_buf[0]=' '; © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page29 AVRDEM 2 PLUS SAMPLE CODE if((fvalue&0x80)==1) { fvalue=~fvalue+1; fahr_buf[0]='-'; } fahr_buf[1]=fvalue/100+48; fahr_buf[2]=(fvalue/10)%10+48; fahr_buf[3]='.'; fahr_buf[4]=fvalue%10+48; fahr_buf[5]='\0'; //Calculate Base Complement } /*********** LM75 Read routine ************************************* END ***********************/ /*********** USART ************************************* Beginning ***********************/ void USART(char *sendbuf) { while ((*sendbuf)!=0) { UDR = *(sendbuf++); while(!(UCSRA & 0x40)) ; UCSRA |= 0x40; } } /*********** USART ************************************* END ***********************/ /*********** Interrupt service routine *************************** Beginning *************/ SIGNAL(SIG_OVERFLOW0) { uchar isr_cyc_cnt,key_data; uint temp; uint uart_cnt; char Cent[]="Centigrade"; char Fahr[]="Fahrenheit"; char Current[]="Current Temperatrue"; TCNT0 = 0xda; // Reload counter value isr_cyc_cnt++; if(isr_cyc_cnt>4)isr_cyc_cnt=0; // Not more than 4-digit data switch(isr_cyc_cnt) { case(0): { LED_Display(0); //Display Characters }break; case(1): DB-DP115_Ver1.0 _Page30 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE { LED_Display(1); }break; case(2): { LED_Display(2); }break; case(3): { LED_Display(3); }break; case(4): { clr_ledcom_port0; clr_ledcom_port1; clr_ledcom_port2; clr_ledcom_port3; uart_cnt++; if((uart_cnt&0xff) == 0xff) { USART(Current); UDR=13; UDR=10; //Display Characters //Display Characters //Display Characters USART(cent_buf); USART(Cent); UDR=13; UDR=10; USART(fahr_buf); USART(Fahr); UDR=13; UDR=10; } else LM75_Temperature(); //Read Temperature Value }break; } if(key_flag==0) //Display one piece of information at a time { key_data=Funckey(); switch(key_data) { case(1): //Display "Sure" { value_thousand=0x6d; value_hundred=0x1c; © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page31 AVRDEM 2 PLUS SAMPLE CODE value_ten=0x50; value_one=0x79; }break; case(2): //Display "1.1" { value_thousand=0x00; value_hundred=0x00; value_ten=0x86; value_one=0x06; }break; case(3): //Display Centigrade Value { temperature_flag=1; LED_Data(cvalue); }break; case(0): //Display Fahrenheit Value { temperature_flag=1; LED_Data(fvalue); }break; } if(Change_key==0)key_flag=1; } if(key_flag==1) //Display information in turn { temp++; if(temp%512==127) //Display "Sure" { value_thousand=0x6d; value_hundred=0x1c; value_ten=0x50; value_one=0x79; } if(temp%512==255) //Display "1.1" { value_thousand=0x00; value_hundred=0x00; value_ten=0x86; value_one=0x06; } if(temp%512==383) //Display Centigrade Value { temperature_flag=1; LED_Data(cvalue); } if(temp%512==0) //Display Fahrenheit Value { temperature_flag=1; DB-DP115_Ver1.0 _Page32 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE LED_Data(fvalue); } if(Func_key==0)key_flag=0; } } /********** Interrupt service routine ********************************** END ********************/ int main (void) { InitDevice(); InitTwi(); LED_InitData(); _delay_ms(10); TCNT0=0; TCCR0=5; TIMSK=_BV(TOIE0); sei(); //prescale: ck/1024 while(1) ; } Sample 8. Display Information on the LCD Module. This Sample shows how to display information on the HD44780 compatible LCD Module. The LCD module was connected to PORTB (as data port), PORTD4 (E signal of LCD module), PORTD3 (RW signal of LCD module), and PORTD2 (RS signal of LCD module), PORTD5 (OCR1A as PWM controlled DC voltage generator). Before using this function, you should install the LCD panel with screws first. Adjust the contrast of the LCD panel via PORTD5. And you could adjust the contrast with potentiometer R28 too. #include <stdint.h> #include <avr/io.h> #include <util/twi.h> #include <avr/delay.h> #include <compat/deprecated.h> #include <avr/interrupt.h> #include <avr/hd44780.h> #define uchar unsigned char #define uint unsigned int © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page33 AVRDEM 2 PLUS SAMPLE CODE #define FREQ 4 #define TempRegAddr 0x00 // Address of Temperature sensor #define ReadLM75Addr 0x91 // Device address is 0. Read #define WriteLM75Addr 0x90 // Write #define Twi_Stop() TWCR=_BV(TWINT)|_BV(TWSTO)|_BV(TWEN) #define Twi_Start() TWCR=_BV(TWINT)|_BV(TWSTA)|_BV(TWEN) #define check_TWINT() while(!(TWCR & (1<<TWINT))) // Wait for TWINT flag being set #define Func_key (PINC&0x80) #define Change_key (PINC&0x40) // Function key // Switch key uint isr_cyc_cnt; uint cvalue,fvalue; // Memory Centigrade and Fahrenheit value char cent_buf[6],fahr_buf[6]; // Centigrade and Fahrenheit value array uchar reset_key_pressed,last_reset_key_pressed; uchar Func_key_pressed,last_Func_key_pressed; uchar change_key_pressed,last_change_key_pressed; uchar LED_thousand,LED_hundred,LED_ten,LED_one; uchar value_thousand,value_hundred,value_ten,value_one; uchar temperature_flag; uchar cnt_fuckey,cnt_chgkey; uchar PWM_data; char PWMbuf[6]; uchar key_flag=0; char pointer=0; uchar TempHigh; uchar TempLow; uchar TempSign,TempData; uchar TwiStatus; char LCD_name[]="Sure Electronics"; char LCD_Ver[]="Ver 1.0"; char Cent[]="Centigrade"; char Fahr[]="Fahrenheit"; char Cont[]="Contrast "; char Current[]="Current Temperature"; //Current Temperature DB-DP115_Ver1.0 _Page34 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE void InitPort(void); void InitUsart(void); void InitTwi(void); void InitDevice(void); void DelayMs(uint ms); void PWM(uchar pwmduty); char FuncKey(void); char ChangeKey(void); void TWITempRead(uchar ReadDeviceAddr,uchar WriteDeviceAddr,uchar RegAddr) ; void LM75_Temperature(void); void USART(uchar *sendbuf); /*********** Initializtion ************************************* Beginning ***********************/ void InitPort(void) { ACSR &=0x7f; DDRA=0xFF; DDRB=0xFF; DDRC=0x3C; // Close Comparator // All PortA is output port // All PORTB is output port // PC7 is for Switch SW1 Input // PC6 is for Switch SW2 Input //PC0,PC1 is for TWI PORTC |=_BV(PORTC6)|_BV(PORTC7)|_BV(PORTC0)|_BV(PORTC1); SFIOR&=~_BV(PUD); DDRD=0xFE; } void InitUsart(void) { UCSRB=0x00; UCSRA=0x00; UBRRH=0x00; UBRRL=0x0c; // USART Baud Rate is 57600bps UCSRC=(1<<URSEL)|(1<<USBS)|(3<<UCSZ0); //Set frame format: 8data, 2stop bit UCSRB=_BV(RXEN)|_BV(TXEN); //Enable reciver and transmitter } © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page35 AVRDEM 2 PLUS SAMPLE CODE void InitDevice(void) { cli(); InitPort(); InitUsart(); } /*********** Initializtion ************************************* END *****************/ /*********** PWM ******************************* Beginning ******************************/ void DelayMs(uint ms) { uint i; for(i=0;i<ms;i++) _delay_loop_2(FREQ*250); } void PWM(uchar pwmduty) { TCCR2=_BV(WGM20)|_BV(COM21)|_BV(CS22); //CLK/64 TCNT2=0; DDRD |= _BV(PD7); OCR2=pwmduty; DelayMs(100); } /*********** PWM ******************************* END ******************************/ /*********** Scan key status ******************************* Beginning *******************/ char FuncKey(void) { uchar temp; if(Func_key==0x80) Func_key_pressed=1; else if(Func_key==0) Func_key_pressed=0; if((Func_key_pressed==0)&(last_Func_key_pressed==1)) { DB-DP115_Ver1.0 _Page36 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE if(Func_key_pressed==0)cnt_fuckey++; } last_Func_key_pressed=Func_key_pressed; temp=cnt_fuckey%3; return temp; } char ChangeKey(void) { uchar temp; if(Change_key==0x40) change_key_pressed=1; else if(Change_key==0) change_key_pressed=0; if((change_key_pressed==0)&(last_change_key_pressed==1)) { if(change_key_pressed==0) cnt_chgkey++; } last_change_key_pressed=change_key_pressed; temp=cnt_chgkey%11; return temp; } /*********** Scan key status ******************************* END ******************************/ /*********** TWI operation routine ********************************** Beginning ***************/ void InitTwi(void) { TWCR= 0X00; //disable twi TWBR= 0x12; //set bit rate TWSR= 0x01; //set prescale TWCR= 0x04; //enable twi } void TWITempRead(uchar ReadDeviceAddr,uchar WriteDeviceAddr,uchar RegAddr) { © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page37 AVRDEM 2 PLUS SAMPLE CODE while (TwiStatus != 0x08) // Check TWSR register. Clear the prescale bit. { Twi_Start(); // Send START signal _delay_us(1); TwiStatus=TWSR & 0xF8; } //StatusLihgting(0); while (TwiStatus != 0x18) // Check TWSR register. SLA+W signal has been //sent, and ACK signal has been received. { TWDR = WriteDeviceAddr; TWCR=0x84; _delay_us(1); TwiStatus=TWSR & 0xF8; // Device Address (write) } while (TwiStatus != 0x28) //DATA has been sent. ACK has been received { TWDR = RegAddr; // Write LM75 address. TWCR=0x84; _delay_us(1); TwiStatus=TWSR & 0xF8; } while (TwiStatus != 0x10) //REPEATED START has been sent. { Twi_Start(); // Send REPEATED START _delay_us(1); TwiStatus=TWSR & 0xF8; } while (TwiStatus != 0x40) // Check TWSR register. SLA+R has been sent. //ACK has been received. { TWDR =ReadDeviceAddr; TWCR=0x84; _delay_us(1); TwiStatus=TWSR & 0xF8; // Device address } TwiStatus=0x00; DB-DP115_Ver1.0 _Page38 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE while (TwiStatus != 0x50) // DATA has been received. ACK has been sent. { TWCR=0xc4; // Receive high 8-bit data. ACK has been sent. _delay_us(1); TempHigh=TWDR; TwiStatus=TWSR & 0xF8; } while(TwiStatus != 0x58) //DATA has been received. NACK has been sent. { TwiStatus=TWSR & 0xF8; TWCR=0x84; // Receive low 8-bit data. nACK has been sent. _delay_us(1); TempLow=TWDR; } Twi_Stop(); // Send STOP } /*********** TWI operation routine ********************************** END *********************/ /*********** LM75 Read routine ******************************** Beginning *******************/ //Read temperature value by LM75 temperature sensor void LM75_Temperature(void) { uint temp_H,temp_L; TWITempRead(ReadLM75Addr,WriteLM75Addr,TempRegAddr); temp_H=TempHigh; //High bits temp_L=TempLow; //Low bits //Compute Centigrade cvalue=(temp_H<<8)|temp_L; cent_buf[0]=' '; if((cvalue&0x80)==1) { cvalue=~cvalue+1; cent_buf[0]='-'; } cvalue=cvalue>>5; cvalue=cvalue * 1.25; cent_buf[1]=cvalue/100+48; © 2004 -2008 Sure Electronics Inc. //Calculate Base Complement DB-DP115_Ver1.0_Page39 AVRDEM 2 PLUS SAMPLE CODE cent_buf[2]=(cvalue/10)%10+48; cent_buf[3]='.'; cent_buf[4]=cvalue%10+48; cent_buf[5]='\0'; //compute Fahrenheit fvalue=((cvalue*9)/5)+32; fahr_buf[0]=' '; if((fvalue&0x80)==1) { fvalue=~fvalue+1; fahr_buf[0]='-'; } fahr_buf[1]=fvalue/100+48; fahr_buf[2]=(fvalue/10)%10+48; fahr_buf[3]='.'; fahr_buf[4]=fvalue%10+48; fahr_buf[5]='\0'; //Calculate Base Complement } /*********** LM75 Read routine ************************************* END ***********************/ /*********** USART ************************************* Beginning ***********************/ void USART(uchar *sendbuf) { while((*sendbuf)!=0) { UDR = *(sendbuf++); while(!(UCSRA & 0x40)) ; UCSRA |= 0x40; } } /*********** USART ************************************* END ***********************/ /************* LCD ************************************ Beginning *******************************/ /* DATA_PORT defines the port to which the LCD data lines are connected */ #define DATA_PORT PORTB #define DATA_PIN PINB #define TRIS_DATA_PORT DDRB DB-DP115_Ver1.0 _Page40 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE /* CTRL_PORT defines the port where the control lines are connected. * These are just samples, change to match your application. */ #define SET_RW_PIN sbi(PORTD,PD3) /* PORT for RW, RW=1 */ #define CLR_RW_PIN cbi(PORTD,PD3) /* RW=0 */ #define SET_TRIS_RW sbi(DDRD,DDD3) /* TRIS for RW */ #define CLR_TRIS_RW cbi(DDRD,DDD3) /* TRIS for RW */ #define SET_RS_PIN sbi(PORTD,PD2) /* PORT for RS, RS=1 */ #define CLR_RS_PIN cbi(PORTD,PD2) /* PORT for RS, RS=0 */ #define SET_TRIS_RS sbi(DDRD,DDD2) /* TRIS for RS */ #define CLR_TRIS_RS cbi(DDRD,DDD2) /* TRIS for RS */ #define SET_E_PIN sbi(PORTD,PD4) /* PORT for E ,1 */ #define CLR_E_PIN cbi(PORTD,PD4) /* PORT for E ,0 */ #define SET_TRIS_E sbi(DDRD,DDD4) /* TRIS for E */ #define CLR_TRIS_E cbi(DDRD,DDD4) /* TRIS for E */ /* Display ON/OFF Control defines */ #define DON 0b00001111 /* Display on */ #define DOFF 0b00001011 /* Display off */ #define CURSOR_ON 0b00001111 /* Cursor on */ #define CURSOR_OFF 0b00001101 /* Cursor off */ #define BLINK_ON 0b00001111 /* Cursor Blink */ #define BLINK_OFF 0b00001110 /* Cursor No Blink */ /* Cursor or Display Shift defines */ #define SHIFT_CUR_LEFT 0b00010011 #define SHIFT_CUR_RIGHT 0b00010111 #define SHIFT_DISP_LEFT 0b00011011 #define SHIFT_DISP_RIGHT 0b00011111 /* Cursor shifts to the left */ /* Cursor shifts to the right */ /* Display shifts to the left */ /* Display shifts to the right */ /* Function Set defines */ #define EIGHT_BIT 0b00111111 /* 8-bit Interface */ #define LINES_5X7 0b00111011 /* 5x7 characters, multiple line */ #define PARAM_SCLASS auto #define MEM_MODEL far /* Change this to near for small memory model */ /* OpenXLCD © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page41 AVRDEM 2 PLUS SAMPLE CODE * Configures I/O pins for external LCD */ void OpenXLCD(unsigned char); /* SetCGRamAddr * Sets the character generator address */ void SetCGRamAddr(unsigned char); /* SetDDRamAddr * Sets the display data address */ void SetDDRamAddr(unsigned char); /* BusyXLCD * Returns the busy status of the LCD */ unsigned char BusyXLCD(void); /* ReadAddrXLCD * Reads the current address */ unsigned char ReadAddrXLCD(void); /* ReadDataXLCD * Reads a byte of data */ char ReadDataXLCD(void); /* WriteCmdXLCD * Writes a command to the LCD */ void WriteCmdXLCD(unsigned char); /* WriteDataXLCD * Writes a data byte to the LCD */ void WriteDataXLCD(char); DB-DP115_Ver1.0 _Page42 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE /* putcXLCD * A putc is a write */ #define putcXLCD WriteDataXLCD /* putsXLCD * Writes a string of characters to the LCD */ void putsXLCD(char *); /* putrsXLCD * Writes a string of characters in ROM to the LCD */ void putsXLCD(char *buffer); /******************************************************************** * Function Name: BusyXLCD * * Return Value: char: busy status of LCD controller * * Parameters: void * * Description: This routine reads the busy status of the * * Hitachi HD44780 LCD controller. * ********************************************************************/ unsigned char BusyXLCD(void) { SET_RW_PIN; // Set the control bits for read BF CLR_RS_PIN; _delay_us(10); SET_E_PIN; // Clock in the command _delay_us(10); if(DATA_PIN&0x80) // Read bit 7 (busy bit) { // If high CLR_E_PIN; // Reset clock line CLR_RW_PIN; // Reset control line return 1; // Return TRUE } else // Bit 7 low { CLR_E_PIN; // Reset clock line CLR_RW_PIN; // Reset control line © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page43 AVRDEM 2 PLUS SAMPLE CODE return 0; // Return FALSE } } /******************************************************************** * Function Name: OpenXLCD * Return Value: void * * Parameters: lcdtype: sets the type of LCD (lines) * * Description: This routine configures the LCD. Based on * * the Hitachi HD44780 LCD controller. The * * routine will configure the I/O pins of the * * microcontroller, setup the LCD for 4- or * * 8-bit mode and clear the display. The user * * must provide three delay routines: * * DelayFor18TCY() provides a 18 Tcy delay * * DelayPORXLCD() provides at least 15ms delay * * DelayXLCD() provides at least 5ms delay * ********************************************************************/ void OpenXLCD(unsigned char lcdtype) { // The data bits are made into inputs * DATA_PIN = 0; TRIS_DATA_PORT = 0x00; SET_TRIS_RW; SET_TRIS_RS; SET_TRIS_E; CLR_RW_PIN; CLR_RS_PIN; CLR_E_PIN; // All control signals made outputs // R/W pin made low // Register select pin made low // Clock pin made low // Delay for more than 15ms to allow for LCD Power on reset _delay_ms(16); // Setup interface to LCD DATA_PORT = 0; TRIS_DATA_PORT = 0xff; DB-DP115_Ver1.0 _Page44 // Data port output © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE DATA_PORT = 0b00110000; CLR_RW_PIN; CLR_RS_PIN; SET_E_PIN; _delay_us(10); CLR_E_PIN; // Function set cmd(8-bit interface) // R/W pin made low // Register select pin made low // Clock the cmd in // Delay for at least 4.1ms _delay_ms(6); // Setup interface to LCD DATA_PORT = 0; DATA_PORT = 0b00110000; CLR_RW_PIN; CLR_RS_PIN; SET_E_PIN; _delay_us(10); CLR_E_PIN; // Function set cmd(8-bit interface) // R/W pin made low // Register select pin made low // Clock the cmd in // Delay for at least 100us _delay_ms(1); // Setup interface to LCD DATA_PORT = 0; DATA_PORT = 0b00110000; SET_E_PIN; _delay_us(10); CLR_E_PIN; TRIS_DATA_PORT = 0x00; // Function set cmd(8-bit interface) // Clock cmd in // Make data port input // Set data interface width, # lines, font while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(lcdtype); // Function set cmd © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page45 AVRDEM 2 PLUS SAMPLE CODE // Turn the display on then off while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(DOFF); // Display OFF/Blink OFF while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(DON); // Display ON/Blink ON while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(BLINK_OFF&CURSOR_OFF); // Display ON/Blink OFF // Clear display while(BusyXLCD()); WriteCmdXLCD(0x01); // Wait if LCD busy // Clear display // Set entry mode inc, no shift while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(SHIFT_CUR_LEFT); // Entry Mode while(BusyXLCD()); WriteCmdXLCD(0b00000110); // Set DD Ram address to 0 while(BusyXLCD()); SetDDRamAddr(0b00000111); // Wait if LCD busy // Set Display data ram address to 0 return; } /******************************************************************** * Function Name: putsXLCD * Return Value: void * Parameters: buffer: pointer to string * Description: This routine writes a string of bytes to the * Hitachi HD44780 LCD controller. The user * must check to see if the LCD controller is * busy before calling this routine. The data * is written to the character generator RAM or * the display data RAM depending on what the DB-DP115_Ver1.0 _Page46 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE * previous SetxxRamAddr routine was called. ********************************************************************/ void putsXLCD(char *buffer) { while(*buffer) // Write data to LCD up to null { while(BusyXLCD()) ; // Wait while LCD is busy WriteDataXLCD(*buffer); // Write character to LCD buffer++; // Increment buffer } return; } /********************************************************************* * Function Name: ReadAddrXLCD * * Return Value: char: address from LCD controller * * Parameters: void * * Description: This routine reads an address byte from the * * Hitachi HD44780 LCD controller. The user * * must check to see if the LCD controller is * * busy before calling this routine. The address* * is read from the character generator RAM or * * the display data RAM depending on what the * * previous SetxxRamAddr routine was called. * *********************************************************************/ unsigned char ReadAddrXLCD(void) { char data; // Holds the data retrieved from the LCD SET_RW_PIN; CLR_RS_PIN; _delay_us(10); SET_E_PIN; _delay_us(10); data = DATA_PIN; © 2004 -2008 Sure Electronics Inc. // Set control bits for the read // Clock data out of the LCD controller // Save the data in the register DB-DP115_Ver1.0_Page47 AVRDEM 2 PLUS SAMPLE CODE CLR_E_PIN; CLR_RW_PIN; return (data&0x7f); // Reset the control lines // Return the address, Mask off the busy bit } /******************************************************************** * Function Name: ReadDataXLCD * Return Value: char: data byte from LCD controller * * Parameters: void * * Description: This routine reads a data byte from the * * Hitachi HD44780 LCD controller. The user * * must check to see if the LCD controller is * * busy before calling this routine. The data * * is read from the character generator RAM or * * the display data RAM depending on what the * * previous SetxxRamAddr routine was called. * ********************************************************************/ char ReadDataXLCD(void) { char data; SET_RS_PIN; SET_RW_PIN; _delay_us(10); SET_E_PIN; _delay_us(10); data = DATA_PIN; CLR_E_PIN; CLR_RS_PIN; CLR_RW_PIN; CLR_E_PIN; _delay_us(10); SET_E_PIN; _delay_us(10); DB-DP115_Ver1.0 _Page48 * // Set the control bits // Clock the data out of the LCD // Read the data // Reset the control bits // Reset the clock line // Clock the next nibble out of the LCD © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE CLR_E_PIN; CLR_RS_PIN; CLR_RW_PIN; return(data); // Reset the control bits // Return the data byte } /******************************************************************** * Function Name: SetCGRamAddr * * Return Value: void * * Parameters: CGaddr: character generator ram address * * Description: This routine sets the character generator * * address of the Hitachi HD44780 LCD * * controller. The user must check to see if * * the LCD controller is busy before calling * * this routine. * ********************************************************************/ void SetCGRamAddr(unsigned char CGaddr) { TRIS_DATA_PORT = 0xff; // Make data port ouput DATA_PORT = CGaddr | 0b01000000; // Write cmd and address to port CLR_RW_PIN; // Set control signals CLR_RS_PIN; _delay_us(10); SET_E_PIN; // Clock cmd and address in _delay_us(10); CLR_E_PIN; _delay_us(10); TRIS_DATA_PORT = 0x00; // Make data port inputs return; } /******************************************************************** * Function Name: SetDDRamAddr * Return Value: void * Parameters: CGaddr: display data address © 2004 -2008 Sure Electronics Inc. * * * DB-DP115_Ver1.0_Page49 AVRDEM 2 PLUS SAMPLE CODE * Description: This routine sets the display data address * * of the Hitachi HD44780 LCD controller. The * * user must check to see if the LCD controller* * is busy before calling this routine. * ********************************************************************/ void SetDDRamAddr(unsigned char DDaddr) { TRIS_DATA_PORT = 0xff; // Make port output DATA_PORT = DDaddr | 0b10000000; // Write cmd and address to port CLR_RW_PIN; // Set the control bits CLR_RS_PIN; _delay_us(10); SET_E_PIN; // Clock the cmd and address in _delay_us(10); CLR_E_PIN; _delay_us(10); TRIS_DATA_PORT = 0; // Make port input return; } /******************************************************************** * Function Name: WriteCmdXLCD * * Return Value: void * * Parameters: cmd: command to send to LCD * * Description: This routine writes a command to the Hitachi* * HD44780 LCD controller. The user must check * * to see if the LCD controller is busy before * * calling this routine. * ********************************************************************/ void WriteCmdXLCD(unsigned char cmd) { TRIS_DATA_PORT = 0xff; // Data port output DATA_PORT = 0; DATA_PORT |= cmd; // Write command to data port CLR_RW_PIN; // Set the control signals CLR_RS_PIN; // for sending a command _delay_us(10); DB-DP115_Ver1.0 _Page50 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE SET_E_PIN; _delay_us(10); CLR_E_PIN; _delay_us(10); TRIS_DATA_PORT = 0x00; // Clock the command in // Data port input return; } /******************************************************************** * Function Name: WriteDataXLCD * * Return Value: void * * Parameters: data: data byte to be written to LCD * * Description: This routine writes a data byte to the * * Hitachi HD44780 LCD controller. The user * * must check to see if the LCD controller is * * busy before calling this routine. The data * * is written to the character generator RAM or* * the display data RAM depending on what the * * previous SetxxRamAddr routine was called. * ********************************************************************/ void WriteDataXLCD(char data) { TRIS_DATA_PORT = 0xff; // Make port output DATA_PORT = data; // Write data to port SET_RS_PIN; // Set control bits CLR_RW_PIN; _delay_us(10); SET_E_PIN; // Clock data into LCD _delay_us(10); CLR_E_PIN; CLR_RS_PIN; // Reset control bits TRIS_DATA_PORT = 0x00; // Make port input return; } void InitLCD(void) { © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page51 AVRDEM 2 PLUS SAMPLE CODE OpenXLCD( EIGHT_BIT&LINES_5X7 );// configure external LCD while(BusyXLCD()) ; SetDDRamAddr(0x00); putsXLCD(LCD_name); // Wait if LCD busy // Set Display data ram address to 0x00 // write to LCD while(BusyXLCD()) ; SetDDRamAddr(0x40); putsXLCD(LCD_Ver); _delay_ms(200); // Wait if LCD busy // Set Display data ram address to 0x40 // write to LCD } void LCD_Display(char *i) { // Set DD Ram address to 0 while(BusyXLCD()) ; SetDDRamAddr(0x40); // write to LCD while(BusyXLCD()) ; putsXLCD(i); // Wait if LCD busy // Set Display data ram address to 0 // Wait if LCD busy } /************** LCD ************************************** END *********************************/ /*************** Timer0 ISR ******************************* Beginning ****************************/ SIGNAL(SIG_OVERFLOW0) { unsigned int fkey_num,ckey_num; TCNT0 = 0x70; //reload counter value isr_cyc_cnt++; LM75_Temperature(); //Read Temperature if(key_flag==0) { fkey_num=FuncKey(); switch(fkey_num) { DB-DP115_Ver1.0 _Page52 © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE case(0): //Display "Centigrade" { LCD_Display(Cent); putsXLCD(cent_buf); PWM_data=fkey_num; }break; case(1): //Display "Fahrenheit" { LCD_Display(Fahr); putsXLCD(fahr_buf); PWM_data=fkey_num; }break; case(2): //Display Contrast { LCD_Display(Cont); PWM_data=fkey_num; }break; } if(Change_key==0)key_flag=1; } if(key_flag==1) { ckey_num=ChangeKey(); if((PWM_data==2)|(PWM_data==3)) { switch(ckey_num) { case(0): { PWMbuf[0]='0'; PWMbuf[1]='%'; PWMbuf[2]=' '; PWMbuf[3]=' '; PWMbuf[4]=' '; PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); © 2004 -2008 Sure Electronics Inc. //Display change // Wait if LCD busy // Set Display data ram address to 0x4b DB-DP115_Ver1.0_Page53 AVRDEM 2 PLUS SAMPLE CODE }break; case(1): { PWMbuf[0]='1'; PWMbuf[1]='0'; PWMbuf[2]='%'; PWMbuf[3]=' '; PWMbuf[4]=' '; PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; case(2): { PWMbuf[0]='2'; PWMbuf[1]='0'; PWMbuf[2]='%'; PWMbuf[3]=' '; PWMbuf[4]=' '; PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; case(3): { PWMbuf[0]='3'; PWMbuf[1]='0'; PWMbuf[2]='%'; PWMbuf[3]=' '; PWMbuf[4]=' '; PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; case(4): { DB-DP115_Ver1.0 _Page54 // Wait if LCD busy // Set Display data ram address to 0x4b // Wait if LCD busy // Set Display data ram address to 0x4b // Wait if LCD busy // Set Display data ram address to 0x4b © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE PWMbuf[0]='4'; PWMbuf[1]='0'; PWMbuf[2]='%'; PWMbuf[3]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; case(5): { PWMbuf[0]='5'; PWMbuf[1]='0'; PWMbuf[2]='%'; PWMbuf[3]=' '; PWMbuf[4]=' '; PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; case(6): { PWMbuf[0]='6'; PWMbuf[1]='0'; PWMbuf[2]='%'; PWMbuf[3]=' '; PWMbuf[4]=' '; PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; case(7): { PWMbuf[0]='7'; PWMbuf[1]='0'; PWMbuf[2]='%'; PWMbuf[3]=' '; PWMbuf[4]=' '; © 2004 -2008 Sure Electronics Inc. // Wait if LCD busy // Set Display data ram address to 0x4b // Wait if LCD busy // Set Display data ram address to 0x4b // Wait if LCD busy // Set Display data ram address to 0x4b DB-DP115_Ver1.0_Page55 AVRDEM 2 PLUS SAMPLE CODE PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; case(8): { PWMbuf[0]='8'; PWMbuf[1]='0'; PWMbuf[2]='%'; PWMbuf[3]=' '; PWMbuf[4]=' '; PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; case(9): { PWMbuf[0]='9'; PWMbuf[1]='0'; PWMbuf[2]='%'; PWMbuf[3]=' '; PWMbuf[4]=' '; PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; case(10): { PWMbuf[0]='1'; PWMbuf[1]='0'; PWMbuf[2]='0'; PWMbuf[3]='%'; PWMbuf[4]=' '; PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); DB-DP115_Ver1.0 _Page56 // Wait if LCD busy // Set Display data ram address to 0x4b // Wait if LCD busy // Set Display data ram address to 0x4b // Wait if LCD busy // Set Display data ram address to 0x4b // Wait if LCD busy // Set Display data ram address to 0x4b © 2004 -2008 Sure Electronics Inc. AVRDEM 2 PLUS SAMPLE CODE putsXLCD(PWMbuf); }break; } } if(Func_key==0)key_flag=0; } if(isr_cyc_cnt%512==0) { PWM(4); USART(Current); UDR=13; UDR=10; PWM(0); _delay_ms(25); USART(cent_buf); _delay_ms(1); USART(Cent); UDR=13; UDR=10; _delay_ms(25); USART(fahr_buf); _delay_ms(1); USART(Fahr); UDR=13; UDR=10; _delay_ms(25); } } /*************** Timer0 ISR ******************************* END ****************************/ int main (void) { InitDevice(); InitTwi(); InitLCD(); TCNT0=0; © 2004 -2008 Sure Electronics Inc. DB-DP115_Ver1.0_Page57 AVRDEM 2 PLUS SAMPLE CODE TCCR0=4; //prescale: ck/1024 TIMSK=_BV(TOIE0); sei(); while(1) ; } DB-DP115_Ver1.0 _Page58 © 2004 -2008 Sure Electronics Inc.
© Copyright 2025