PHP Code:
#include <avr/io.h>
#include <stdio.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include "lcd_lib.c"
#include "lcd_lib.h"
/*******************************************
// doc gia tri tra ve tu counter, xuat ra LCD
// xuat gia tri len PC
//tinh ra toc do motor
//xuat toc do le LCD
*******************************************/
uint16_t rad=0; //khai bao bien toan cuc
uint16_t t = 0;
//dat chuong trinh con phuc vu ngat o truoc
ISR (TIMER1_OVF_vect)
{
TCNT1=54736; //gia tri TCNT1 nap lai
t++;
}
//adjust LCDsendChar () function for stream
static int LCDsendstream (char c, FILE *stream);
//current channel
//uint8_t ch;
//set output stream to LCD
static FILE lcd_str = FDEV_SETUP_STREAM (LCDsendstream, NULL, _FDEV_SETUP_WRITE);
//string for LCD store in Flash memory
const uint8_t motor2 [] PROGMEM="motor2: \0";
const uint8_t motor1 [] PROGMEM="motor1: \0";
const uint8_t CLRROW [] PROGMEM=" \0"; //clear row
const uint8_t *LCDXY [] = {
motor1, //for Motor1
motor2}; //for Motor2
static int LCDsendstream (char c, FILE *stream)
{
LCDsendChar (c);
return 0;
}
void init_counter0 ()
{
//set external clock source on T0 pin, clock on rising edge
TCCR0=(0<<FOC0)|(1<<CS02)|(1<<CS01)|(1<<CS00);
}
void init_timer1 ()
{
//set timer1 prescaling to 1024
//prescaling cang lon thi f cang nho
//f cang nho thi t cang lon
//thoi gian ma cang lon thi cang lau
//moi lan TCNT1 tang len 1 don vi duoc 92.6us
//mot lan clock duoc 185 us
//test voi 92.6us
//moi lan timer1 tran duoc 1s, chinh xac
TCCR1B=(1<<CS12)|(0<<CS11)|(1<<CS10);
TCNT1=54736;
// enable interrupt
TIMSK=(1<<TOIE1);
}
void init_avr ()
{
init_counter0();
init_timer1();
stdout = &lcd_str;
LCDinit ();
LCDclr ();
LCDGotoXY(0,0);
//adc_init ();
}
int main ()
{
uint16_t speed=0;
DDRB=0xfe;
init_avr (); //inti LCD trong ham init_avr
//LCDsendCommand(0);
LCDstring(" Loading...",13);
//enable global interrupt
sei();
while (1)
{
if (TIFR&(1<<TOV0))
{
//1 lan tran duoc 1/2 vong
rad++;
TIFR=(1<<TOV0);
}
if (t==30)
{
speed=rad;
LCDsendCommand(0);
CopyStringtoLCD (CLRROW, 0, 0);
CopyStringtoLCD (LCDXY[(0)], 0, 0);
printf ("%d", speed);
t=rad=0;
}
}
}
và lỗi cả file delay.h
mình dùng AtmelStudio 6
Trong khai báo có file lcd_lib.c và file lcd_lib.h mình có down 2 file đó về rồi thì phải bỏ vào thư mục nào để chương trình chạy được. Trong Atmelstudio không thấy file delay.h hay file delay.c có down file delay.h về rồi mà ko biết nhét vào đâu để chạy được.
Cho hỏi cái project này có áp dụng vào hiển thị vòng tua máy của xe máy được không ?
Comment