o nhit dng DS18B20,o khong cch, giao tip my tnh, hin th LCD.
Li m: Mnh hy vng ti ny s gip cho bn hiu thm v lp trnh cho Avr. Mnh bc u lm quen vi Avr v gp nhiu kh khn, mt nhiu time (v c $) cho vic lm quen, mong rng ti liu ny s gip bn tip cn Avr nhanh hn .Tt c phn cng v code mnh lm v chy OK. Cui cng xin cm n din n dientuvietnam.net cho mnh hc hi rt nhiu kin thc. c bit l mod Sphinx,( xin cm n nhn s ca Pnlab :D ) CH : Ti liu ny ch dnh cho newbie ,ch cc bc cao th m c th em ly lm xu h lm. Cc trang web b ch cho vn ny : www.dientuvietnam.net http://vagam.dieukhien.net/
Cc linh kin trong mch: Chip AVR Atmega16. IC DS18B20. IC GP2D12. MAX 232. LCD 1602 LM7805. Led, tr, loa t.
C l khng phi ni nhiu v mch ny Mnh ch xin cp 1 s phn nh : Kt ni vi ds18B20 bn c th xem datasheet Trong mch c jump ca mch np , c th b i v cho vo mch s phc tp hn. Code mnh dng thch anh trong 4Mhz nn bn c th b thch anh v t 22p nu dng thch anh trong. Mnh dng adc chn adc0 o khong cch t con Gp2D12, nu bn ko dng con ny th c th coi code nh l adc bnh thng. V phn cng ch c vy. Bn c th tham kho thm file mch mnh gi km.
Phn mm: By gi lp trnh no, mnh chia lm 2 phn : trn AVR (mnh dng CodeVisionAvr)v trn PC (dng Vb6 :D).
CodeVision AVR:
Mnh kt hp adc v ds18B20 b li (cng khng bit ti sao, bn gii quyt c nh ch cho mnh nh) nn mnh lp trnh 2 code ring 1 code o nhit v 1 code ADC.
Sau y l code o nhit : Duongv3@gmail.com Lests Share to be Shared
5
Click chn new Project :
V
D nhin l Yes ri Sau bn ln lt cu hnh nh sau:
Chn chip
Duongv3@gmail.com Lests Share to be Shared
6
Ni LCD vo PORT B
Duongv3@gmail.com Lests Share to be Shared
7
Cu hnh ds18B20
Duongv3@gmail.com Lests Share to be Shared
8
Cu hnh USART
Duongv3@gmail.com Lests Share to be Shared
9
Cu hnh cng OUT cho cc PORTA.2 v PORTA.4 bo hiu ra LED v Loa
Sau vo menu vo nh trong hnh ri chn tn file save Duongv3@gmail.com Lests Share to be Shared
10
By gi ta c code c/t t dch . u tin l phi thay dng #include <ds1820.h> bng #include <ds18b20.h> (n gin v ta dng IC 18B20).
Cui cng y l code mnh vit
/***************************************************** This program was produced by the CodeWizardAVR V2.03.4 Standard Automatic Program Generator Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.com
Project : Version : Date : 6/23/2009 Author : DuongV3 Company : DHBK Comments: Have A Mice Day
Chip type : ATmega16 Program type : Application Clock frequency : 4.000000 MHz Memory model : Small External RAM size : 0 Data Stack size : 256 *****************************************************/
#if RX_BUFFER_SIZE<256 unsigned char rx_wr_index,rx_rd_index,rx_counter; #else unsigned int rx_wr_index,rx_rd_index,rx_counter; #endif
// This flag is set on USART Receiver buffer overflow bit rx_buffer_overflow;
// USART Receiver interrupt service routine interrupt [USART_RXC] void usart_rx_isr(void) { char status,data; status=UCSRA; data=UDR; if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0) { rx_buffer[rx_wr_index]=data; if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0; if (++rx_counter == RX_BUFFER_SIZE) { rx_counter=0; rx_buffer_overflow=1; }; }; }
#ifndef _DEBUG_TERMINAL_IO_ // Get a character from the USART Receiver buffer #define _ALTERNATE_GETCHAR_ #pragma used+ char getchar(void) { char data; while (rx_counter==0); Duongv3@gmail.com Lests Share to be Shared
#if TX_BUFFER_SIZE<256 unsigned char tx_wr_index,tx_rd_index,tx_counter; #else unsigned int tx_wr_index,tx_rd_index,tx_counter; #endif
// USART Transmitter interrupt service routine interrupt [USART_TXC] void usart_tx_isr(void) { if (tx_counter) { --tx_counter; UDR=tx_buffer[tx_rd_index]; if (++tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0; }; }
#ifndef _DEBUG_TERMINAL_IO_ // Write a character to the USART Transmitter buffer #define _ALTERNATE_PUTCHAR_ #pragma used+ void putchar(char c) { while (tx_counter == TX_BUFFER_SIZE); #asm("cli") if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0)) Duongv3@gmail.com Lests Share to be Shared
// Standard Input/Output functions #include <stdio.h>
// Declare your global variables here void putnum(int a) { switch(a){ case 0: putchar('0'); break; case 1: putchar('1'); break; case 2: putchar('2'); break; case 3: putchar('3'); break; case 4: putchar('4'); break; case 5: putchar('5'); break; case 6: putchar('6'); break; case 7: putchar('7'); break; case 8: putchar('8'); break; case 9: putchar('9'); break;
Duongv3@gmail.com Lests Share to be Shared
15
}; }
int getnum(unsigned char a) { switch(a){ case 0: return 0; break; case 1: return 1; break; case 2:return 2; break; case 3: return 3; break; case 4: return 4; break; case 5: return 5; break; case 6: return 6; break; case 7: return 7; break; case 8: return 8; break; case 9: return 9; break;
}; } void locso(float so) { // unsigned char t,c,d,tp1,tp2,tp3,tp4; float temp1,temp2; // t=so/100; // lay fan tram c=so/10; // lay fan chuc d=(so-10*c); // lay hang don vi if(so<0) Duongv3@gmail.com Lests Share to be Shared
// 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=0xD8; UCSRC=0x86; UBRRH=0x00; UBRRL=0x19;
// Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; SFIOR=0x00;
// 1 Wire Bus initialization w1_init();
// LCD module initialization lcd_init(16); Duongv3@gmail.com Lests Share to be Shared
19
// Global enable interrupts #asm("sei")
lcd_clear(); lcd_gotoxy(0,0); lcd_putsf("DuongV3"); lcd_gotoxy(0,1); lcd_putsf("T(C)= "); define_char(char0,0); delay_ms(1000); i=0; max=35; min=30; while (1) { if (rx_counter>0){ kt=getchar(); //kt tra ve se la gia tri trong bang ASCii // lcd_putchar(kt); array[i]=getnum(kt)-48; ++i; } if(i==2) { max=array[0]*10+array[1];
} if(i==4) { i=0; min=array[2]*10+array[3]; array[0]=0; array[1]=0; array[2]=0; array[3]=0; } // Chon so max, min if (max<min) { t_g=max; Duongv3@gmail.com Lests Share to be Shared
20
max=min; min=t_g; } //In ra LCD Gia tri max min sprintf(lcd_buffer,"%d",max); lcd_gotoxy(0,0); lcd_putsf("Max= "); lcd_puts(lcd_buffer);
By gi mnh xin gii thch v lu thut ton trong hm main v vng while(1) (ngi v lm nn ch ni thi). u tin c/t s t khi to cho ta ds18b20 v lcd.Sau vit ln dng u tin mn hnh dng ch DuongV3 (ci ny th tt nhin bn s thay i thi :D). Sau l vit ln dng th 2 dng ch t(c) . t gi tr i , max , min ban u cnh bo. Chi tit v lp trnh LCD, bn tham kho trang sau : http://www.vagam.dieukhien.net/discuss.php?thid=3121 http://vagam.dieukhien.net/discuss.php?thid=148&pagenum=1
By gi l trong vng while: Nhn k t t cng Com(nu c) ghi 2 k t nhn c vo mng (2 k t ny s coi l 1 s max hoc min) Sau nhn tip 2 k t na lu vo mng (2 k t ny s coi l 1 s max hoc min).,Hm getnum l mnh vit c tc dng chuyn t k t sang s lu vo mng so snh 2 s v chn max ,min Ghi ra LCD Nu khi to ban u cho ds18b20 thnh cng gn tg cho nhit t con ds18B20 Nu dng ghi du +, nu m ghi du - ,a gi tr ra LCD locso() c tc dng chuyn s nhit c dng xx.xxxx v cc s ring phn chc , dv, thp phn tng ng vi cc bin c,d,tp1,tp2,tp3,tp4 Hm putnum () mnh vit c tc dng a 1 s ra cng COM . Vy l ht nh :D Ch cu hnh hm sprintf() nh sau:
Duongv3@gmail.com Lests Share to be Shared
22
Vo Project configure:
Chn nh sau :
Duongv3@gmail.com Lests Share to be Shared
23
Sau y l code ADC :
Cu hnh ging trn cc phn ca chip, lcd , PORT cn ADC cu hnh nh sau :
y mnh dng ADC 10bit nn nn ko tick vo Use 8 bits. V mnh chn chn ADC vo l ADC0 nn n s scan First =0 v Last = 0 Cui cng y l code phn ny.
/* function used to define user characters */ void define_char(byte flash *pc,byte char_code) { byte i,a; a=(char_code<<3) | 0x40; for (i=0; i<8; i++) lcd_write_byte(a++,*pc++); }
#if RX_BUFFER_SIZE<256 unsigned char rx_wr_index,rx_rd_index,rx_counter; #else unsigned int rx_wr_index,rx_rd_index,rx_counter; #endif
// This flag is set on USART Receiver buffer overflow bit rx_buffer_overflow;
// USART Receiver interrupt service routine interrupt [USART_RXC] void usart_rx_isr(void) { char status,data; status=UCSRA; data=UDR; if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0) { rx_buffer[rx_wr_index]=data; if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0; if (++rx_counter == RX_BUFFER_SIZE) { rx_counter=0; rx_buffer_overflow=1; }; }; }
#ifndef _DEBUG_TERMINAL_IO_ // Get a character from the USART Receiver buffer #define _ALTERNATE_GETCHAR_ #pragma used+ char getchar(void) { char data; while (rx_counter==0); data=rx_buffer[rx_rd_index]; if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0; #asm("cli") --rx_counter; #asm("sei") Duongv3@gmail.com Lests Share to be Shared
#if TX_BUFFER_SIZE<256 unsigned char tx_wr_index,tx_rd_index,tx_counter; #else unsigned int tx_wr_index,tx_rd_index,tx_counter; #endif
// USART Transmitter interrupt service routine interrupt [USART_TXC] void usart_tx_isr(void) { if (tx_counter) { --tx_counter; UDR=tx_buffer[tx_rd_index]; if (++tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0; }; }
#ifndef _DEBUG_TERMINAL_IO_ // Write a character to the USART Transmitter buffer #define _ALTERNATE_PUTCHAR_ #pragma used+ void putchar(char c) { while (tx_counter == TX_BUFFER_SIZE); #asm("cli") if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0)) { tx_buffer[tx_wr_index]=c; if (++tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0; ++tx_counter; } Duongv3@gmail.com Lests Share to be Shared
27
else UDR=c; #asm("sei") } #pragma used- #endif
// Standard Input/Output functions #include <stdio.h>
// ADC interrupt service routine // with auto input scanning interrupt [ADC_INT] void adc_isr(void) { static unsigned char input_index=0; // Read the AD conversion result adc_data[input_index]=ADCW; // Select next ADC input if (++input_index > (LAST_ADC_INPUT-FIRST_ADC_INPUT)) input_index=0; ADMUX=(FIRST_ADC_INPUT | (ADC_VREF_TYPE & 0xff))+input_index; // Delay needed for the stabilization of the ADC input voltage delay_us(10); // Start the AD conversion ADCSRA|=0x40; }
void putnum(int a) { switch(a){ case 0: putchar('0'); break; case 1: putchar('1'); Duongv3@gmail.com Lests Share to be Shared
28
break; case 2: putchar('2'); break; case 3: putchar('3'); break; case 4: putchar('4'); break; case 5: putchar('5'); break; case 6: putchar('6'); break; case 7: putchar('7'); break; case 8: putchar('8'); break; case 9: putchar('9'); break;
}; }
void locso(int so) { // unsigned char t,c,d,tp1,tp2,tp3,tp4; // float temp1,temp2; t=so/100; // lay fan tram c=(so-100*t)/10; // lay fan chuc d=(so-100*t-10*c); // lay hang don vi }
void main(void) { unsigned int adc; unsigned char lcd_buffer[10]; Duongv3@gmail.com Lests Share to be Shared
// Timer/Counter 1 initialization // Clock source: System Clock // Clock value: Timer 1 Stopped Duongv3@gmail.com Lests Share to be Shared
30
// Mode: Normal top=FFFFh // OC1A output: Discon. // OC1B output: Discon. // Noise Canceler: Off // Input Capture on Falling Edge // Timer 1 Overflow Interrupt: Off // Input Capture Interrupt: Off // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off TCCR1A=0x00; TCCR1B=0x00; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00;
Tt nhin , khi lp trnh bn phi thay gi tr ca mng cng nh code trong vng while . Code trn PC (phn ny nu qua thi v c trong Folder ri): Dim s As String
Private Sub Command1_Click() s = MSComm1.Input 'Temp Text1.Text = s End Sub
Private Sub Command2_Click() s = MSComm1.Input Text2.Text = s End Sub
Private Sub Command3_Click() s = Text3.Text ' Max MSComm1.Output = s End Sub
Private Sub Command4_Click() s = Text4.Text 'Min MSComm1.Output = s End Sub
Duongv3@gmail.com Lests Share to be Shared
34
Private Sub Command5_Click() End End Sub
Private Sub Form_Load() MSComm1.CommPort = 1 MSComm1.Settings = "9600,n,8,1" MSComm1.PortOpen = True End Sub
Cch dng : cho 2 s c 2 ch s vo MAX , MIN sau click vo Button truyn xung LCD . Click vo nhiet do c nhiet do. Ch : dng ci ny , phi tt cc ti nguyn ang dng cng Com nh mch np (c th) hoc terminal ca CodeVision. V file .exe ca mnh bt ln rt l lu mi ln (cng ko bit ti sao, KIS bo l virus :D ) Cui cng l 1 s ch (c th bn lm lm ging ht trn cho vo mch ko chy th ch 1 s li hay gp sau): 1. iu u tin l xem li mch, kim tra cc chn LCD v PORTB, AVR vi MAX232, MAX 232 vi cng COM .. ,Ban u mnh truyn ton k t li , ha ra hn cng Com cha chc ( nht l chn ni GND ) :D 2. Bn cu hnh cho hm sprintf cha ?. Duongv3@gmail.com Lests Share to be Shared
35
3. Cu hnh cho dao ng 4Mhz dao ng trong cho AVR . Mnh dng mch np AVR 910 dng AVRProg cu hnh nh sau : 4. Clcick vo Advance
Chn nh trn , ri Chip erase Write Duongv3@gmail.com Lests Share to be Shared
36
V nu l mch np khc , bn phi chn Fuse bits, chi tit xem ti trang sau: (v tn trng bn quyn m nn mnh s khng ni): http://www.dientuvietnam.net/forums/showthread.php?t=4182 Mnh ch bn 1 bng trong datasheet thi:
V
5. Ci code Vision rt l kh hiu,ban u bn vit code OK sau ch thay i 1 s th n khng chy em tr v gi tr ban u ln ny ko chy tht. V vy kinh nghim ca mnh l vit code OK th copy, coi l ver 1 sau mun sa g th sa . VD nh : vi code Ok nu bn thay thanh ghi UBRRL v UBRRH th phn giao tip my tnh s ko chy (tt nhin), bn a n v gi tr ban u n s vn ko chy:D. ,m nhn y ni giao tip my tnh, mnh ni lun cch tnh UBRRL v UBRRH khi chn thch anh . Cng thc s l Duongv3@gmail.com Lests Share to be Shared
37
UBRR =( Tn s thch anh /9600*16) -1. Sau s cho vo phn High v low ca UBRR ( y l s h 16 (0x) ). 6. Cui cng nu n vn ko chy, hy lin lc vi mnh , mnh s c gng gip :D duongv3@gmail.com . 7. V na l : Code mnh vit khng hon ton ti u, c trn vi iu khin ln PC (nht l trn PC , mnh vit vi trong 1 m :D ) . Bn nu ci tin th share lun cho mnh mnh hc tp nh. M qun, ngay c bi ny mnh cng vit vi nn nu sai v chnh t, bn b qua nh ^-^