源代码: #include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "TWI_I2C.h"
#define Smg_a 0x80
#define Smg_b 0x40
#define Smg_c 0x20
#define Smg_d 0x01
#define Smg_e 0x02
#define Smg_f 0x08
#define Smg_g 0x04
#define Smg_dp 0x10
#define Bmp0Map Smg_a | Smg_b | Smg_c | Smg_d | Smg_e | Smg_f
#define Bmp1Map Smg_b | Smg_c
#define Bmp2Map Smg_a | Smg_b | Smg_d | Smg_e | Smg_g
#define Bmp3Map Smg_a | Smg_b | Smg_c | Smg_d | Smg_g
#define Bmp4Map Smg_b | Smg_c | Smg_f | Smg_g
#define Bmp5Map Smg_a | Smg_c | Smg_d | Smg_f | Smg_g
#define Bmp6Map Smg_a | Smg_c | Smg_d | Smg_e | Smg_f | Smg_g
#define Bmp8Map Smg_a | Smg_b | Smg_c | Smg_d | Smg_e | Smg_f | Smg_g
#define Bmp9Map Smg_a | Smg_b | Smg_c | Smg_d | Smg_f | Smg_g
#define Bmp7Map Smg_a | Smg_b | Smg_c
#define BmpAMap Smg_a | Smg_b | Smg_c | Smg_e | Smg_f | Smg_g
#define BmpBMap Smg_c | Smg_d | Smg_e | Smg_f | Smg_g
#define BmpCMap Smg_a | Smg_d | Smg_e | Smg_f
#define BmpDMap Smg_b | Smg_c | Smg_d | Smg_e | Smg_g
#define BmpEMap Smg_a | Smg_d | Smg_e | Smg_f | Smg_g
#define BmpFMap Smg_a | Smg_e | Smg_f | Smg_g
#define CS_0 PORTB&=0XFE
#define CS_1 PORTB|=0X1
#define WR_0 PORTD&=0XDF
#define WR_1 PORTD|=0X20
#define DA_0 PORTC&=0XFE
#define DA_1 PORTC|=0X1
const unsigned char SegCode[] = {Bmp0Map,Bmp1Map,Bmp2Map,Bmp3Map,Bmp4Map,Bmp5Map,Bmp6Map,Bmp7Map,Bmp8Map,Bmp9Map,BmpAMap,BmpBMap,BmpCMap,BmpDMap,BmpEMap,BmpFMap};
unsigned char DpyNum[6] = {Smg_g,Smg_g,Smg_g,Smg_g,Smg_g,Smg_g}; //显示缓冲区
unsigned int i;
void delay(void)
{
unsigned char i;
for(i=0;i<50;i++);
}
void COMMAND_HT1621(unsigned char comm)
{
unsigned char i;
CS_0;
//delay();
WR_0;
//delay();
for(i=0;i<3;i++)
{
if((0x04<<i)&0x04) DA_1; //100
else DA_0;
WR_0;
WR_1;
}
for(i=0;i<9;i++)
{
if((comm<<i)&0x80) DA_1; //100
else DA_0;
WR_0;
WR_1;
}
CS_1;
}
void HT1621_Display(void)
{
unsigned char i;
CS_0;
//delay();
WR_0;
//delay();
for(i=0;i<3;i++)
{
if((0x05<<i)&0x04) DA_1;
else DA_0;
WR_0;
WR_1;
}
for(i=0;i<6;i++)
{
if((20<<i)&0x20) DA_1;
else DA_0;
WR_0;
WR_1;
}
for(i=0;i<8;i++)
{
if((DpyNum[5]<<i)&0x80) DA_1;
else DA_0;
WR_0;
WR_1;
}
for(i=0;i<8;i++)
{
if((DpyNum[4]<<i)&0x80) DA_1;
else DA_0;
WR_0;
WR_1;
}
for(i=0;i<8;i++)
{
if((DpyNum[3]<<i)&0x80) DA_1;
else DA_0;
WR_0;
WR_1;
}
for(i=0;i<8;i++)
{
if((DpyNum[2]<<i)&0x80) DA_1;
else DA_0;
WR_0;
WR_1;
}
for(i=0;i<8;i++)
{
if((DpyNum[1]<<i)&0x80) DA_1;
else DA_0;
WR_0;
WR_1;
}
for(i=0;i<8;i++)
{
if((DpyNum[0]<<i)&0x80) DA_1;
else DA_0;
WR_0;
WR_1;
}
CS_1;
}
void LCD_Init(void)
{
COMMAND_HT1621(0x01); //SYS EN
COMMAND_HT1621(0x03); //LCD ON
COMMAND_HT1621(0x29); //4 COM 1/3 BIAS
HT1621_Display(); //clear screen
}
unsigned char I2C_Read_Bytes(unsigned char address,unsigned char *databuf,unsigned char len)
{
unsigned char posi;
I2C_Start();//I2C
//Delay_1ms();
I2C_Wait();
if (I2C_TestAck()!=I2C_START)
return 1; //ACK
I2C_Write8Bit(WD_DEVICE_ADDR); //
I2C_Wait();
if (I2C_TestAck()!=MT_SLA_ACK)
return 1; //ACK
I2C_Write8Bit(address); //
I2C_Wait();
if (I2C_TestAck()!=MT_DATA_ACK)
return 1;
I2C_Start(); //
I2C_Wait();
if (I2C_TestAck()!=I2C_RESTART)
return 1;
I2C_Write8Bit(RD_DEVICE_ADDR); //
I2C_Wait();
if(I2C_TestAck()!=MR_SLA_ACK)
return 1; //ACK
for(posi=0;posi<len-1;posi++)
{
I2C_Twi_Ack(); //
I2C_Wait();
if(I2C_TestAck()!=MR_DATA_ACK)
return 1; //ACK
*(databuf+posi)=TWDR;//
}
I2C_Twi_NoAck(); //
I2C_Wait();
if(I2C_TestAck()!=MR_DATA_NOACK)
return 1; //ACK
*(databuf+posi)=TWDR;//
I2C_Stop();//I2C
return 0;
}
unsigned char I2C_Write_Bytes(unsigned char address,unsigned char *databuf,unsigned char len)
{
unsigned char posi;
I2C_Start(); //I2C
I2C_Wait();
if(I2C_TestAck()!=I2C_START)
return 1; //ACK
I2C_Write8Bit(WD_DEVICE_ADDR); //
I2C_Wait();
if(I2C_TestAck()!=MT_SLA_ACK)
return 1; //ACK
I2C_Write8Bit(address); //
I2C_Wait();
if(I2C_TestAck()!=MT_DATA_ACK)
return 1; //ACK
for(posi=0;posi<len;posi++)
{
I2C_Write8Bit(*(databuf+posi)); //
I2C_Wait();
if(I2C_TestAck()!=MT_DATA_ACK)
return 1; //ACK
}
I2C_Stop(); //I2C
_delay_ms(10); //
return 0;
}
void TWI_Init(void)
{
TWBR=0x03; //I2C
TWSR|=(1<<TWPS0);
}
void main(void)
{
unsigned char buffer[3],bai,shi,ge;
long int i=0;
DDRB=0XFF;
DDRC=0XCF;
DDRD=0XFF;
LCD_Init();
TWI_Init();
DpyNum[0] = Smg_a|Smg_d|Smg_g;
DpyNum[1] = SegCode[0];
DpyNum[2] = SegCode[0];
DpyNum[3] = SegCode[3];
DpyNum[4] = SegCode[4];
DpyNum[5] = SegCode[5];
HT1621_Display();
buffer[0]=69;
buffer[1]=126;
buffer[2]=229;
I2C_Write_Bytes(0,buffer,3);
I2C_Read_Bytes(0,buffer,1);
bai=buffer[0]/100;
shi=buffer[0]%100/10;
ge=buffer[0]%10;
DpyNum[3] = SegCode[bai];
DpyNum[4] = SegCode[shi];
DpyNum[5] = SegCode[ge];
HT1621_Display();
for(i=0;i<300000;i++);
I2C_Read_Bytes(1,buffer,1);
bai=buffer[0]/100;
shi=buffer[0]%100/10;
ge=buffer[0]%10;
DpyNum[3] = SegCode[bai];
DpyNum[4] = SegCode[shi];
DpyNum[5] = SegCode[ge];
HT1621_Display();
for(i=0;i<300000;i++);
I2C_Read_Bytes(2,buffer,1);
bai=buffer[0]/100;
shi=buffer[0]%100/10;
ge=buffer[0]%10;
DpyNum[3] = SegCode[bai];
DpyNum[4] = SegCode[shi];
DpyNum[5] = SegCode[ge];
HT1621_Display();
while(1)
{
}
} 头文件: #ifndef TWI_I2C_H
#define TWI_I2C_H
#define I2C_START 0x08
#define I2C_RESTART 0x10
#define MT_SLA_ACK 0x18
#define MT_SLA_NOACK 0x20
#define MT_DATA_ACK 0x28
#define MT_DATA_NOACK 0x30
#define MR_SLA_ACK 0x40
#define MR_SLA_NOACK 0x48
#define MR_DATA_ACK 0x50
#define MR_DATA_NOACK 0x58
#define I2C_Start() (TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN))
#define I2C_Stop() (TWCR=(1<<TWINT)|(1<<TWSTO)|(1<<TWEN))
#define I2C_Wait() {while(!(TWCR&(1<<TWINT)));}
#define I2C_TestAck() (TWSR&0xf8)
#define I2C_SetAck (TWCR|=(1<<TWEA))
#define I2C_SetNoAck (TWCR&=~(1<<TWEA))
#define I2C_Twi_NoAck() (TWCR=(1<<TWINT)|(1<<TWEN))
#define I2C_Twi_Ack() (TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA))
#define I2C_Write8Bit(x) {TWDR=(x);TWCR=(1<<TWINT)|(1<<TWEN);}
#define RD_DEVICE_ADDR 0xA1
#define WD_DEVICE_ADDR 0xA0
#endif
硬件I2C,通过HT1621显示
资料仅供同学学习使用
|