You are on page 1of 37

Duongv3@gmail.

com Lests Share to be Shared



1

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/

LCD:
1. http://dientuvietnam.net/forums/showthread.php?t=281&highlight=L%E1%BA%ADp
+tr%C3%ACnh+hi%E1%BB%83n+th%E1%BB%8B+LCD+b%E1%BA%B1ng+Asse
mbler+%28+tutorial%29
2. http://www.vagam.dieukhien.net/discuss.php?thid=3121&pagenum=2
3. http://vagam.dieukhien.net/discuss.php?thid=148&pagenum=1
RS232
4. http://www.8051projects.net/serial-communication/introduction.php
5. http://www.vagam.dieukhien.net/discuss.php?thid=3216&pagenum=1
6. http://dientuvietnam.net/forums/forumdisplay.php?f=74





Duongv3@gmail.com Lests Share to be Shared

2

Phn cng :
Di y l ci mch ca mnh .







Duongv3@gmail.com Lests Share to be Shared

3

Mch tht n (xu qu :D):










Duongv3@gmail.com Lests Share to be Shared

4

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
*****************************************************/

#include <mega16.h>
#include <delay.h>

// 1 Wire Bus functions
#asm
.equ __w1_port=0x15 ;PORTC
.equ __w1_bit=0
#endasm
#include <1wire.h>

// DS1820 Temperature Sensor functions
Duongv3@gmail.com Lests Share to be Shared

11

#include <ds18b20.h>

// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>
int c,d,tp1,tp2,tp3,tp4;
typedef unsigned char byte;

flash byte char0[8]={
0b0000000,
0b0011100,
0b0010100,
0b0011100,
0b0000000,
0b0000000,
0b0000000,
0b0000000};
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++);
}


#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
Duongv3@gmail.com Lests Share to be Shared

12


// USART Receiver buffer
#define RX_BUFFER_SIZE 8
char rx_buffer[RX_BUFFER_SIZE];

#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

13

data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif

// USART Transmitter buffer
#define TX_BUFFER_SIZE 8
char tx_buffer[TX_BUFFER_SIZE];

#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

14

{
tx_buffer[tx_wr_index]=c;
if (++tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0;
++tx_counter;
}
else
UDR=c;
#asm("sei")
}
#pragma used-
#endif

// 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

16

temp2=so*-1;
else
temp2=so;

temp1=temp2-(c*10+d);
tp1 = (unsigned char)(temp1*10);
tp2 = (unsigned char)(temp1*100-tp1*10);
tp3 = (unsigned char)(temp1*1000-tp1*100-tp2*10);
tp4 = (unsigned char)(temp1*10000-tp1*1000-tp2*100-tp3*10);

}

// unsigned char t,c,d,tp1,tp2,tp3,tp4;
void main(void)
{
float tg;
char lcd_buffer[10];
unsigned char kt;
int min,max,t_g,array[4],i;
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=Out Func3=In Func2=Out Func1=In
Func0=In
// State7=T State6=T State5=T State4=0 State3=T State2=0 State1=T State0=T
PORTA=0x00;
DDRA=0x14;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
Duongv3@gmail.com Lests Share to be Shared

17

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// 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;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
Duongv3@gmail.com Lests Share to be Shared

18

// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// 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);

sprintf(lcd_buffer,"%d",min);
lcd_gotoxy(8,0);
lcd_putsf("Min= ");
lcd_puts(lcd_buffer);
//
if(ds18b20_init(0,25,35,DS18B20_12BIT_RES))
{
tg= ds18b20_temperature(0);
// temp=(unsigned char) (tg);

if(tg==-9999)
{
}
else
{

lcd_gotoxy(6,1);
if(tg<0)
lcd_putchar(45);
else
lcd_putchar(43);
sprintf(lcd_buffer,"%.4f",tg);
lcd_puts(lcd_buffer);
lcd_putchar(0);
lcd_putsf("C");
delay_ms(80);
if(tg>max)
PORTA=0x10; //PORTA=0x14;
else if (tg<min)
PORTA=0x14;
else PORTA=0x00;
Duongv3@gmail.com Lests Share to be Shared

21

}
}
locso(tg);

putnum(c);
putnum(d);
putchar('.');
putnum(tp1);
putnum(tp2);
putnum(tp3);
putnum(tp4);
putchar(' ');

};
}

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.


#include <mega16.h>

// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>
int t,c,d;
typedef unsigned char byte;

Duongv3@gmail.com Lests Share to be Shared

24

flash byte char0[8]={
0b0111111,
0b0100001,
0b0100001,
0b1110001,
0b0100001,
0b0100001,
0b0100001,
0b0011111};





/* 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++);
}

#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// USART Receiver buffer
#define RX_BUFFER_SIZE 8
char rx_buffer[RX_BUFFER_SIZE];

Duongv3@gmail.com Lests Share to be Shared

25

#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

26

return data;
}
#pragma used-
#endif

// USART Transmitter buffer
#define TX_BUFFER_SIZE 8
char tx_buffer[TX_BUFFER_SIZE];

#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>

#include <delay.h>

#define FIRST_ADC_INPUT 0
#define LAST_ADC_INPUT 0
unsigned int adc_data[LAST_ADC_INPUT-FIRST_ADC_INPUT+1];
#define ADC_VREF_TYPE 0x00

// 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

29

int
a[22]={497,464,420,385,360,330,307,290,266,248,230,224,211,209,198,194,189,182,17
8,170,167,167 },kc,i;
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x10;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// 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;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

Duongv3@gmail.com Lests Share to be Shared

31

// 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;

// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AREF pin
// ADC Auto Trigger Source: None
ADMUX=FIRST_ADC_INPUT | (ADC_VREF_TYPE & 0xff);
ADCSRA=0xCA;

// LCD module initialization
lcd_init(16);

// Global enable interrupts
#asm("sei")
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("DuongV3");

// define_char(char0,0);
// define_char(char1,1);
// define_char(char2,2);
// lcd_putchar(0);
/* lcd_putsf("i");
lcd_putchar(1);
Duongv3@gmail.com Lests Share to be Shared

32

lcd_putsf("n t");
lcd_putchar(2);*/

while (1)
{
adc = adc_data[0];

for(i=0;i<=19;i++)
{
if(adc< a[i] && adc >a[i+1])
{
kc= i+10;
lcd_gotoxy(0,1);
break;
}
}
// lcd_gotoxy(0,1);
if(adc> a[0] || adc <a[19])
{ //lcd_clear();
lcd_gotoxy(0,1);
lcd_putsf("Ngoai Khoang ");
delay_ms(100);
}
else //if(adc< a[0] && adc >a[29])
{
sprintf(lcd_buffer,"Distance :%d",kc);
lcd_gotoxy(0,1);
lcd_puts(lcd_buffer);
lcd_putsf(" cm ");

locso(kc);

putnum(c);
putnum(d);
putchar(' ');
delay_ms(100);
};
};
}

Duongv3@gmail.com Lests Share to be Shared

33

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 ^-^


Lests Share to be Shared !!!!!!!

You might also like