最近弄FPGA弄得我一个头三个大的,由于不熟悉Verilog,基础不扎实,所以老是弄了几个钟都没弄什么出来,所以很郁闷。今天下午突然又想弄起之前的AVR,于是就把之前的12864显示改进了一下,加了DS18B20,对于C我是比较熟悉的,所以很快就把程序给搞定了 呵 终于搞出一点实在的东西出来了 可惜不是FPGA的 唉 不说这个啦 先把程序弄上来先。。。
#include<iom16v.h>
#include<macros.h>
#define uchar unsigned char
#define uint unsigned int
#define PB0 0
#define PB1 1
#define PB2 2
#define PB3 3
#define PSB_0 PORTB &= ~(1 << PB1)
#define PSB_1 PORTB |= (1 << PB1)
#define EN_0 PORTB &= ~(1 << PB0)
#define EN_1 PORTB |= (1 << PB0)
#define RW_0 PORTB &= ~(1 << PB2)
#define RW_1 PORTB |= (1 << PB2)
#define RS_0 PORTB &= ~(1 << PB3)
#define RS_1 PORTB |= (1 << PB3)
#define DATAPORT PORTA
uchar t;
uchar temh,teml;
uchar Flag_1820Error=0;
void delay_1ms()
{
uint i;
for(i=1;i<1140;i++);
}
void delay_nms(uint n)
{
uint i="0";
while(i<n)
{
delay_1ms();
i++;
}
}
void m16_init()
{
PORTA="0x00";
DDRA="0xff";
PORTB="0x00";
DDRB="0xff";
PORTC="0xff";
DDRC="0xff";
}
void wcode(uchar command)
{
delay_nms(18);
RS_0;
delay_nms(5);
RW_0;
delay_nms(5);
DATAPORT="command";
delay_nms(5);
EN_1;
delay_nms(5);
EN_0;
}
void wdata(uchar data)
{
delay_nms(18);
RS_1;
delay_nms(5);
RW_0;
delay_nms(5);
DATAPORT="data";
delay_nms(5);
EN_1;
delay_nms(5);
EN_0;
}
void lcd_pos(uchar X,uchar Y)
{
uchar pos;
if(X==0)
{X=0x80;}
else if(X==1)
{X=0x90;}
else if(X==2)
{X=0x88;}
else if(X==3)
{X=0x98;}
pos="X"+Y;
wcode(pos);
}
void lcd_init()
{
PSB_1;
NOP();
NOP();
wcode(0x30);
delay_nms(5);
wcode(0x0c);
delay_nms(5);
wcode(0x01);
delay_nms(5);
}
void writeString(uchar * str)
{
uchar i = 0;
while(str != '\0')
{
wdata(str[i++]);
}
}
void delay_5us(void) //5us左右
{
uchar x="7";
while(x)
{
x--;
}
}
void delay_15us(void) //15us左右
{
uchar x="27";
while(x)
{
x--;
}
}
void delay_60us(void) //60us左右
{
uchar x="117";
while(x)
{
x--;
}
}
void init_1820(void)
{
uchar i;
uint j="0";
PORTD|=(1<<7); //"1"
PORTD&=~(1<<7); //"0"
for(i=0;i<8;i++)delay_60us();//480us以上
PORTD|=(1<<7); //"1"
DDRD&=~(1<<7); //"PINC7 is INPUT"
delay_15us(); //15~60us
delay_15us();
Flag_1820Error=0;
while(PIND&(1<<7))
{ delay_60us();
j++;
if(j>=18000){Flag_1820Error=1;break;}
}
DDRD|=(1<<7); //PORTC7 is OUTPUT
PORTD|=(1<<7); //"1"
for(i=0;i<4;i++)delay_60us(); //240us
}
void write_1820(uchar x)
{
uchar m;
for(m=0;m<8;m++)
{
if(x&(1<<m)) //写数据,从低位开始
{PORTD&=~(1<<7);delay_5us(); //"0",5us
PORTD|=(1<<7); //write"1"
delay_15us(); //15~45us
delay_15us();
delay_15us();
}
else
{PORTD&=~(1<<7);delay_15us();//"0",15us
delay_15us(); //write"0"
delay_15us(); //15~45us
delay_15us();
PORTD|=(1<<7); //"1"
}
}
PORTD|=(1<<7); //"1"
}
uchar read_1820(void)
{
uchar temp,k,n;
temp="0";
for(n=0;n<8;n++)
{
PORTD&=~(1<<7); //"0"
delay_5us();
PORTD|=(1<<7); //"1"
delay_5us();
DDRD&=~(1<<7); //"PINC7 is INPUT"
k=(PIND&(1<<7)); //读数据,从低位开始
if(k)
temp|=(1<<n); //read"1"
else
temp&=~(1<<n); //read"0"
delay_15us(); //45us
delay_15us();
delay_15us();
DDRD|=(1<<7); //PORTC7 is OUTPUT
}
return (temp);
}
void read_temperature(void)
{
uchar tempval;
init_1820(); //复位18b20
write_1820(0xcc); // 发出转换命令
write_1820(0x44);
delay_nms(100);
init_1820();
write_1820(0xcc); //发出读命令
write_1820(0xbe);
teml="read"_1820(); //读数据byte1
temh="read"_1820(); //byte2
teml=teml>>4; //低字节右移4位,去掉小数位
t=temh<<4; //高字节左移4位,去掉符号位
t|=teml; //合并整数部分
}
void main()
{
uchar i;
m16_init();
lcd_init();
init_1820();
while(1)
{
lcd_pos(0,0);
writeString("2009年09月09日");
lcd_pos(1,0);
writeString("星期六15:15:15");
lcd_pos(2,0);
writeString("温度 ℃" );
read_temperature();
lcd_pos(2,2);
wdata(t/10+0x30);
wdata(t%10+0x30);
}
}
文章评论(0条评论)
登录后参与讨论