Mình đang cần người giúp mình hoàn thiện đề tài.Mọi ý kiến gửi về
namdinhcuong2702@gmail.com
sourecode
Link download (phiên bản Demo)
File Kèm Theo
#include
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & BORDIS & IESODIS & FCMDIS & LVPDIS);
#include
#include
#include
#include "lcd.h"
void spi_init();
unsigned char spi(unsigned char data);
void xeeprom_write(unsigned int addr, unsigned char data);
unsigned char xeeprom_read(unsigned int addr);
#define CS RD0
/*
Ghi 1 byte gia tri 0x55 vao dia chi 0x00 cua EEPROM 25LC256
Doc 1 byte tu dia chi 0x00 cua EEPROM 25LC256 ve
trong 1 bien va kiem tra tren MPLAB (PITKit2)
*/
char Counter;
float a, b, c, delta,x1,x2;
void main()
{
//khoi tao
spi_init();
lcd_init();
//Cau hinh I/O
TRISB0 = 1;
WPUB0 = 1;//weak-pullup
RBPU = 0;
TRISB1 = 1;
WPUB1 = 1;//weak-pullup
RBPU = 0;
TRISB2 = 1;
WPUB2 = 1;//weak-pullup
RBPU = 0;
Counter = xeeprom_read(0x0005);
if(Counter==0xFF) Counter = 0;
while(1){
lcd_gotoxy(0,0);
printf("he so A ");
while(RB2){ if(RB0 == 0) {
Counter++;
lcd_gotoxy(0,0);
printf("\fA(tang) :\n%d ",Counter);
xeeprom_write(0x0006,Counter);
while(!RB0);
}
if(RB1 == 0) {
Counter--;
lcd_gotoxy(0,0);
printf("\fA(giam):\n%d ",Counter);
xeeprom_write(0x0006,Counter);
while(!RB1);
}
while(!RB2);
}
lcd_gotoxy(0,0);
printf("\he so B ");
while(RB2){ if(RB0 == 0) {
Counter++;
lcd_gotoxy(0,0);
printf("\fB(tang): \n%d ",Counter);
xeeprom_write(0x0001,Counter);
while(!RB0);
}
if(RB1 == 0) {
Counter--;
lcd_gotoxy(0,0);
printf("\fB(giam):\n%d ",Counter);
xeeprom_write(0x0001,Counter);
while(!RB1);
}
while(!RB2);
}
lcd_gotoxy(0,0);
printf("he so C ");
while(RB2){ if(RB0 == 0) {
Counter++;
lcd_gotoxy(0,0);
printf("\fC(tang):\n%d ",Counter);
xeeprom_write(0x0002,Counter);
while(!RB0);
}
if(RB1 == 0) {
Counter--;
lcd_gotoxy(0,0);
printf("\fC(giam):\n%d ",Counter);
xeeprom_write(0x0002,Counter);
while(!RB1);
}
while(!RB2);
}
a = xeeprom_read(0x0006);
b = xeeprom_read(0x0001);
c = xeeprom_read(0x0002);
delta = b * b - 4 * a * c;
if (delta<0){lcd_gotoxy(1,0);
printf(" vo nghiem");
}
else if (delta == 0)
{
lcd_gotoxy(1,0);
printf("\n x1 = x2 = %f", -b/(2*a));
}
else
{
lcd_gotoxy(1,0);
printf(" x1 = %f", (-b + sqrt(delta))/(2*a));
lcd_gotoxy(1,4);
printf("\nx2 = %f", (-b - sqrt(delta))/(2*a));
}
while(!RB2);
}
}
void putch(char c)
{
lcd_putc(c);
}
void spi_init()
{
ANSEL = ANSELH = 0;
// IO PORT
TRISC3 = 0; // SCK la output
TRISC5 = 0; // SDO la output
TRISC4 = 1; // SDI la input
TRISD0 = 0; // 25LC256 chip select
CS = 1; // deselect chip
//SSPSTAT
SMP = 1; // lay mau o giua chu ky bus
CKE = 1; // du lieu ra tren SDO theo canh lenh cua SCK
//SSPCON
SSPCON = 0b00100000; // CKP = 0, SPI mode baudrate = Fosc/4
}
unsigned char spi(unsigned char data)
{
WCOL = 0; // clear any write collision condition
SSPIF = 0; // clear SSP interrupt flag
SSPBUF = data; // load the SSP buffer with data
while(!SSPIF); // wait for SSPIF to be set
return SSPBUF;
}
void xeeprom_write(unsigned int addr, unsigned char data) {
//ghi lenh WREN
CS = 0;
spi(0x06);
CS = 1;
_delay(100);
//Ghi byte 0x55 vao dia chi 0x0001
CS = 0;
spi(0x02); //lenh WRITE
spi((unsigned char)(addr>>8)); //Dia chi can ghi 0x0000
spi((unsigned char)(addr));
spi(data); //Du lieu can ghi
CS = 1;
}
unsigned char xeeprom_read(unsigned int addr)
{
unsigned char temp;
CS = 0;
spi(0x03); //Lenh READ
spi((unsigned char)(addr>>8)); //Dia chi can ghi 0x0000
spi((unsigned char)(addr));
temp = spi(0x00); //Ghi 1 byte va nhan ve 1 byte
CS=1;
return temp;
}
No comments:
Post a Comment