การแสดงผลด้วย LCD
โปรแกรมนี้ได้ดัดแปลงตัวอย่างและเรียกใช้ฟังก์ชั่นควบคุมการทำงานของ LCD จาก http://www.mikrocontroller.net/articles/AVR-GCC-Tutorial/LCD-Ansteuerung
1. สร้างโปรเจคใหม่ และเพิ่มคำสั่งในส่วนของการจำลองการทำงานของ LCD
Xdisp LCD(24 2 250K) PD4 VSS PD5 PD3 PD2 PD1 PD0 nc3 nc2 nc1 nc0
ซึ่งมีความหมายดังนี ;X[inst_name] LCD(chars lines oscil_freq) RS RW E D7 D6 D5 D4 D3 D2 D1 D0
2. เขียนโปรแกรมส่งข้อมูล
#include <avr\io.h> // Most basic include files #include <avr/interrupt.h> // Add the necessary ones #include <avr\signal.h> // here #include "lcd_routines.c" int main ( void ) { // initialize the LCD LCD_Init ( ) ; // Print character
lcd_data( 'I' ); lcd_data( ' ' ); lcd_data( 'a' ); lcd_data( 'm' ); lcd_data( ' ' ); lcd_data( 'w' ); lcd_data( 'r' ); lcd_data( 'i' ); lcd_data( 't' ); lcd_data( 'i' ); lcd_data( 'n' ); lcd_data( 'g' ); lcd_data( ' ' ); lcd_data( 't' ); lcd_data( 'o' ); // Set cursor in the 2nd row lcd_setcursor ( 0 , 2 ) ; // print string lcd_string("Thank you"); while ( 1 ) { } return 0 ; }
ตัวอย่างการอ่านค่า Analog ผ่าน ADC แสดงผลทาง LCD
สัญญาณอนาลอก อ่านทาง ADC ช่อง 0
1. สร้างโปรเจคใหม่ และเพิ่มคำสั่งในส่วนของการจำลองการทำงานของ LCD และ Analog input ด้วย Slider
Xdisp LCD(24 2 250K) PD4 VSS PD5 PD3 PD2 PD1 PD0 nc3 nc2 nc1 nc0
R1 VDD AREF 1 ;Set reference voltage
Rin0 PC0 node_v0 100K ; conect Slider-1 to PC0 (Analog ch0)
Vin0 node_v0 VSS SLIDER_1(0 5) ; Slider 1 in Control Panel from 0 to 5V
2. เขียนโปรแกรมอ่านค่า ADC และแสดงผลออกทาง LCD
#include <avr\io.h> // Most basic include files
#include <avr\interrupt.h> // Add the necessary ones
#include <avr\signal.h> // here
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lcd_routines.c"
unsigned int adc0;
char buffer [20]; // locally in this {}
float volt = 0.0;
char n;
// ***********************************************************
// Main program
//
int main(void)
{
ADCSRA = (1<<ADEN)|(0<<ADATE); // ADC Enable & Auto Trigger Disable
ADCSRA |= (0<<ADPS2)|(0<<ADPS1)|(1<<ADPS0); // XTAL/8
lcd_init();
while(1)
{
ADMUX = 0b0000000; //Aref,left adjust, select ADC0 (bit 2=0 bit 1 = 0 bit 0 = 0)
ADCSRA |= (1<<ADSC); // ADC Start Conversion
while (!(ADCSRA &(1<<ADIF))); // Wait Coversion completes
adc0 = ADCW; // Read ADC
itoa (adc0, buffer, 10);
lcd_setcursor( 0, 1);
lcd_string("Analog = ");
lcd_string (buffer);
lcd_string(" ");
lcd_setcursor( 0, 2 );
lcd_string("Voltage = ");
volt = (float)((adc0*5.0)/1024.0);
dtostrf(volt,1,3, buffer); // converts the double value passed in val into an ASCII
lcd_string (buffer);
lcd_string(" ");
_delay_ms(10);
}
}