#include "stm32f4xx.h"
#include "delay.h"
#include "usart2.h"
#include "usart1.h"
#include "HT1621.h"
/******************端口定义********************/
#define DATA1 PAout(4)
#define WR1 PAout(2)
#define RD1 PAout(1)
#define CS1 PAout(0)
u8 buffer1[20]; //接收串口缓冲寄存器
u16 USART_Sta; //接收计数
int seg[11]={0x0a0f,0x0006,0x060d,0x040f,0x0c06,0x0c0b,0x0e0b,0x000e,0x0e0f,0x0c0f,0x0a0f};
/**************段码真值表****************/
void Seg1(void);
void Seg2(void);
void Seg3(void);
void Seg4(void);
void Seg5(void);
void Seg6(void);
void My_USART1_Init(void); //串口1初始化
void DelayMS(unsigned int iMs);
void HT1621_Init(void); //1621初始化
void Delay(unsigned char us);
void HT1621_Port_Init(void); //1621的GPIO初始化
void Ht1621Wr_Data(u8 Data,u8 cnt); //向1621写 cnt个 位
void Ht1621WrOneData(u8 Addr,u8 Data); //向1621的某地址写四个数据
void Ht1621WrAllData(void); //点亮所有的显示段
void Ht1621WrCmd(u8 Cmd); //1621写命令函数
//void read_data(u8 data,u8 len);
//u8 read_OneData(u8 addr);
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
My_USART1_Init();
My_USART2_Init();
My_USART3_Init();
HT1621_Port_Init();
HT1621_Init();
while(1);
}
void Delay(unsigned char us) //5,7,9
{
while(--us);
}
void DelayMS(unsigned int iMs)
{
unsigned int i,j;
for(i=0;i<iMs;i++)
for(j=0;j<65;j++)
{
Delay(1);
}
}
void HT1621_Port_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd =GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void Ht1621Wr_Data(u8 Data,u8 cnt)
{
u8 i;
CS1=0;
for (i=0;i<cnt;i++)
{
WR1=0;
DelayMS(3);
if((Data & 0x80)==0x80)
{
DATA1=1;
}
else
{
DATA1=0;
}
WR1=1;
DelayMS(3);
Data<<=1;
}
}
void Ht1621WrOneData(uc8 Addr,u8 Data)
{
CS1=0;
DelayMS(1);
Ht1621Wr_Data(0xa0,3); //D′è?êy?Y±ê??101
Ht1621Wr_Data(Addr<<2,6); //D′è?μ??·êy?Y
Ht1621Wr_Data(Data<<4,4); //D′è?êy?Y
CS1=1;//
}
void Ht1621WrAllData()
{
u8 i;
CS1=0;//
Ht1621Wr_Data(0xa0,3); //D′è?êy?Y±ê??101
Ht1621Wr_Data(0x00<<2,6); //D′è?μ??·êy?Y
for (i=0;i<16;i++)
{
Ht1621Wr_Data(0xff ,8);//0x00,8); //D′è?êy?Y
}
CS1=1;//
}
void Ht1621WrCmd(u8 Cmd)
{
CS1=0;//HT1621_CS=0;
DelayMS(1);
Ht1621Wr_Data(0x80,4); //??????100
Ht1621Wr_Data(Cmd,8); //??????
CS1=1;//HT1621_CS=1;
}
void HT1621_Init(void)
{
CS1=1;
WR1=1;
DATA1=1;
DelayMS(20);
Ht1621WrCmd(0x52); //BIAS
Ht1621WrCmd(0x30); //?ú2?ê±?ó256K
Ht1621WrCmd(0x00); //SYDIS
Ht1621WrCmd(0x0A); //WDTDIS
Ht1621WrCmd(0x02); //SYSEN
Ht1621WrCmd(0x06); //LCDON
DelayMS(20); //?óê±20ms
Ht1621WrAllData();
}
void My_USART1_Init(void)
{
GPIO_InitTypeDef a;
USART_InitTypeDef USRAT_Structure; //?????
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //????2??
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //??GPIOA??
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //??2???UART2
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //??3???UART2
a.GPIO_Pin =GPIO_Pin_9|GPIO_Pin_10;
a.GPIO_Mode=GPIO_Mode_AF;
a.GPIO_OType =GPIO_OType_PP;
a.GPIO_PuPd =GPIO_PuPd_UP;
a.GPIO_Speed =GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&a);
USRAT_Structure.USART_BaudRate =115200;//?????
USRAT_Structure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//?????
USRAT_Structure.USART_Mode=USART_Mode_Rx | USART_Mode_Tx;//?????
USRAT_Structure.USART_Parity=USART_Parity_No;//?????
USRAT_Structure.USART_StopBits=USART_StopBits_1;//?????
USRAT_Structure.USART_WordLength =USART_WordLength_8b;//?????
USART_Init(USART1,&USRAT_Structure);//?????
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//
NVIC_InitStructure.NVIC_IRQChannelSubPriority =1; //
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //
NVIC_Init(&NVIC_InitStructure); //
}
void USART1_IRQHandler(void)
{
u8 Res;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //?óê??D??(?óê?μ?μ?êy?Y±?D?ê?0x0d 0x0a?á?2)
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //?áè??óê?μ?μ?êy?Y
if(Res==0x3b) //?óê?μ?á?0x3b
{
USART_Sta=0;
Seg1();
Seg2();
Seg3();
Seg4();
Seg5();
Seg6();
}
else
{
buffer1[USART_Sta]=Res;
USART_Sta++;
}
}
}
void Seg1(void)
{
int Data,num1,num2;
u8 m,n;
Data=buffer1[0];
num1=Data/10;
num2=Data%10;
m=(seg[num1]>>8)&0xff;
n=(seg[num1]&0x00ff)&0xff;
Ht1621WrOneData(0x00,m);
Ht1621WrOneData(0x01,n);
m=(seg[num2]>>8)&0xff;
n=(seg[num2]&0x00ff)&0xff;
Ht1621WrOneData(0x02,m);
Ht1621WrOneData(0x03,n);
}
void Seg2(void)
{
int Data,num1,num2;
u8 m,n;
Data=buffer1[1];
num1=Data/10;
num2=Data%10;
m=(seg[num1]>>8)&0xff;
n=(seg[num1]&0x00ff)&0xff;
Ht1621WrOneData(0x04,m);
Ht1621WrOneData(0x05,n);
m=(seg[num2]>>8)&0xff;
n=(seg[num2]&0x00ff)&0xff;
Ht1621WrOneData(0x06,m);
Ht1621WrOneData(0x07,n);
}
void Seg3(void)
{
int Data,num1,num2;
u8 m,n;
Data=buffer1[2];
num1=Data/10;
num2=Data%10;
m=(seg[num1]>>8)&0xff;
n=(seg[num1]&0x00ffd)&0xff;
Ht1621WrOneData(0x08,0x00);
Ht1621WrOneData(0x09,0x00);
m=(seg[num2]>>8)&0xff;
n=(seg[num2]&0x00ff)&0xff;
Ht1621WrOneData(0x0a,m);
Ht1621WrOneData(0x0b,n);
}
void Seg4(void)
{
int data,h,l;
data=buffer1[4];
h=(data&0xf0)>>4;
if(buffer1[4]&0x01)
l=l|0x08;
else l=l&0x07;
if(buffer1[3]&0x01)
l=l|0x04;
else l=l&0x0b;
if(buffer1[3]&0x20)
l=l|0x02;
else l=l&0x0d;
if(buffer1[3]&0x10)
l=l|0x01;
else l=l&0x0e;
Ht1621WrOneData(0x0c,h);
Ht1621WrOneData(0x0d,l);
}
void Seg5(void)
{
int data,h,l;
data=buffer1[3];
if(buffer1[3]&0x08)
h=h|0x02;
if(buffer1[3]&0x02)
h=h|0x08;
if(buffer1[3]&0x04)
h=h|0x04;
if(buffer1[4]&0x08)
h=h|0x01;
l=l|0x08;
if(buffer1[4]&0x02)
l=l|0x04;
else l=l&0x0b;
if(buffer1[4]&0x04)
l=l|0x02;
else l=l&0x0d;
l=l|0x01;
Ht1621WrOneData(0x0e,h);
Ht1621WrOneData(0x0f,l);
}
void Seg6(void)
{
int data,h,l;
data=buffer1[5];
h=(data&0xf0)>>4;
l=data&0x0f;
Ht1621WrOneData(0x10,h);
Ht1621WrOneData(0x11,l);
}
|