| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- /*
- * File: CH455.c
- * Author: JiangXiaowei
- */
-
- #include "ch455h.h" // 修改该文件以适应硬件环境/单片机型号等
- //#include "delay.h"
- #include "systick.h"
-
- //定义
- #define I2C_DELAY 10
-
- const UINT8 BCD_decode_tab[0x10] = {
-
- 0x3F,//0011 1111B,0
- 0x06,//0000 0110B,1
- 0x5B,//0101 1011B,2
- 0x4F,//0100 1111B,3
- 0x66,//0110 0110B,4
- 0x6D,//0110 1101B,5
- 0x7D,//0111 1101B,6
- 0x27,//0010 0111B,7
- 0x7F,//0111 1111B,8
- 0x6F,//0110 1111B,9
-
- 0x77,//0011 0111B,A
- 0x7C,//0111 1100B,B
- 0x58,//0011 1001B,C
- 0x5E,//0101 1110B,D
- 0x79,//0111 1001B,E
- 0x71 //0111 0001B,F
- };//BCD
-
-
- const UINT8 Letter_tab[38] = {
- 0x3F,//0011 1111B,0
- 0x06,//0000 0110B,1
- 0x5B,//0101 1011B,2
- 0x4F,//0100 1111B,3
- 0x66,//0110 0110B,4
- 0x6D,//0110 1101B,5
- 0x7D,//0111 1101B,6
- 0x27,//0010 0111B,7
- 0x7F,//0111 1111B,8
- 0x6F,//0110 1111B,9
- 0x77,//0011 0111B,A
- 0x7C,//0111 1100B,B
- 0x58,//0011 1001B,C
- 0x5E,//0101 1110B,D
- 0x79,//0111 1001B,E
- 0x71,//0111 0001B,F
- 0x3D,//0011 1101B,G
- 0x76,//0111 0110B,H
- 0x10,//0001 0000B,I
- 0x0E,//0000 1110B,J
- 0x7A,//0111 1010B,K
- 0x38,//0011 1000B,L
- 0x55,//0101 0101B,M
- 0x54,//0101 0100B,N
- 0x5C,//0101 1100B,O
- 0x73,//0111 0011B,P
- 0x67,//0110 0111B,Q
- 0x50,//0101 0000B,R
- 0x65,//0110 0101B,S
- 0x78,//0111 1000B,T
- 0x3E,//0011 1110B,U
- 0x1C,//0001 1100B,V
- 0x6A,//0110 1010B,W
- 0x64,//0110 0100B,X
- 0x6E,//0110 1110B,Y
- 0x5A,//0101 1010B,Z
- 0x40,//0100 0000B,-
- 0x00//0000 0000B,灭
- };//BCD
-
-
-
- void CH455_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE ); //使能GPIOB时钟
-
-
- //stm32f103中PB3、PB4、PA15为JTAG引脚,若要作为普通IO口来使用的话,要禁用JTAG功能。
- //RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
- //GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_SetBits(GPIOB,GPIO_Pin_10|GPIO_Pin_11);
-
- CH455_Write( CH455_SYSON_8 ); //八段式
-
- EXTI_MY_INIT();
-
- }
- void CH455H_SCL_D_OUT(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD ; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- delay_us(I2C_DELAY);
- }
- void CH455H_SDA_D_OUT(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD ; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- delay_us(I2C_DELAY);
- }
- void CH455H_SDA_D_IN(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING ; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- delay_us(I2C_DELAY);
- }
- /*
- * 函数功能:IIC产生起始信号函数
- * 输入参数:无
- * 输出参数:无
- */
- void CH455_I2c_Start(void)
- {
- CH455_SDA_D_OUT;
- CH455_SCL = high; //SCL = high;
- delay_us(I2C_DELAY);
- CH455_SDA = high; //SDA = high;
- delay_us(I2C_DELAY);
- CH455_SDA = low; //SDA = low;
- delay_us(I2C_DELAY);
- CH455_SCL = low; //SCL = low;
- delay_us(I2C_DELAY);
- }
-
- /*
- * 函数功能:IIC产生停止信号
- * 输入参数:无
- * 输出参数:无
- */
- void CH455_I2c_Stop(void)
- {
- CH455_SDA_D_OUT;
- CH455_SCL = low; //SCL = low;
- delay_us(I2C_DELAY);
- CH455_SDA = low; //SDA = low;
- delay_us(I2C_DELAY);
- CH455_SCL = high; //SCL = high;
- delay_us(I2C_DELAY);
- CH455_SDA = high; //SDA = high;
- delay_us(I2C_DELAY);
- }
- UINT8 IIC_Wait_Ack(void)
- {
- UINT8 ucErrTime=0;
- CH455_SDA_D_IN; //SDA设置为输入
- //CH455_SDA=1;delay_us(1);
- CH455_SCL=0;delay_us(1);
- while(READ_SDA)
- {
- ucErrTime++;
- if(ucErrTime>250)
- {
- CH455_I2c_Stop();
- return 1;
- }
- delay_us(1);
- }
- //CH455_SCL=0;//时钟输出0
- return 0;
- }
- /*
- * 函数功能:IIC发送一个字节
- * 输入参数:一个字节
- * 输出参数:无
- */
- void CH455_I2c_WrByte(unsigned char IIC_Byte)
- {
- unsigned char i;
- CH455_SDA_D_OUT;
- for(i = 0; i < 8; i++)
- {
- if(IIC_Byte & 0x80)
- CH455_SDA = high; //SDA=high;
- else
- CH455_SDA = low; //SDA=low;
- delay_us(I2C_DELAY);
- CH455_SCL = high; //SCL=high;
- delay_us(I2C_DELAY);
- CH455_SCL = low; //SCL=low;
- delay_us(I2C_DELAY);
- IIC_Byte<<=1;
- }
- delay_us(I2C_DELAY);
- CH455_SDA = high; //SDA=1;
- delay_us(I2C_DELAY);
- CH455_SCL = high; //SCL=1;
- delay_us(I2C_DELAY);
- CH455_SCL = low; //SCL=0;
- delay_us(I2C_DELAY);
- }
-
- unsigned char CH455_I2c_RdByte( void ) //读一个字节数据
- {
- unsigned char i, bytedata;
- CH455_SDA_D_IN; //将数据设置为输入模式
- delay_us(I2C_DELAY);
- CH455_SDA = 1; //数据线拉高
- delay_us(I2C_DELAY);
- CH455_SCL = 0;
- delay_us(I2C_DELAY);
- for(i = 0; i < 8; i++) //读8位数据
- {
- CH455_SCL = 1;
- delay_us(I2C_DELAY);
- bytedata <<= 1;
- delay_us(I2C_DELAY);
- bytedata |= (CH455_SDA_IN);
- delay_us(I2C_DELAY);
- CH455_SCL = 0;
- delay_us(I2C_DELAY);
- }
- CH455_SDA_D_OUT; //数据线设置回输出模式
- CH455_SDA = high; //SDA=1;
- delay_us(I2C_DELAY);
- CH455_SCL = high; //SCL=1;
- delay_us(I2C_DELAY);
- CH455_SCL = low; //SCL=0;
- delay_us(I2C_DELAY);
- return(bytedata);//返回数据
- }
-
- void CH455_Write( UINT16 cmd ) //写命令
- {
- CH455_I2c_Start(); //启动总线
- CH455_I2c_WrByte(((UINT8)(cmd>>7)&CH455_I2C_MASK)|CH455_I2C_ADDR);
- CH455_I2c_WrByte((UINT8)cmd); //发送数据
- CH455_I2c_Stop(); //结束总线
- }
-
- UINT8 CH455_Read( void ) //读取按键
- {
- UINT8 keycode;
- CH455_I2c_Start(); //启动总线
- CH455_I2c_WrByte((UINT8)(CH455_GET_KEY>>7)&CH455_I2C_MASK|0x01|CH455_I2C_ADDR);
- keycode=CH455_I2c_RdByte(); //读取数据
- CH455_I2c_Stop(); //结束总线
- return keycode;
- }
- void EXTI_MY_INIT(void)
- {
-
- GPIO_InitTypeDef GPIO_InitStructure;
- EXTI_InitTypeDef EXTI_InitStructure;
-
- NVIC_InitTypeDef NVIC_InitStructure;
-
- //GPIO的复用功能必须打开
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- //配置外部中断源并进行中断源和GPIO的连接
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource5);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
- EXTI_InitStructure.EXTI_Line =EXTI_Line5|EXTI_Line1;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
-
- NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //使能按键所在的外部中断通道
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级2,
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //子优先级1
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
- NVIC_Init(&NVIC_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn; //使能按键所在的外部中断通道
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级2,
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; //子优先级1
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
- NVIC_Init(&NVIC_InitStructure);
- }
- volatile UINT8 USER_BUTTON = 0;
- void EXTI1_IRQHandler(void)
- {
- USER_BUTTON = 1;
- EXTI_ClearITPendingBit(EXTI_Line1); //清除LINE11线路挂起位
- }
- void EXTI9_5_IRQHandler(void)
- {
-
- if(KEY3==0)
- {
- USER_BUTTON = 3;
- EXTI_ClearITPendingBit(EXTI_Line5); //清除LINE5线路挂起位
- }
- // if(KEY4==0)
- // {
- // USER_BUTTON = 4;
- // EXTI_ClearITPendingBit(EXTI_Line6); //清除LINE6线路挂起位
- // }
-
- }
|