ch455h.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * File: CH455.c
  3. * Author: JiangXiaowei
  4. */
  5. #include "ch455h.h" // 修改该文件以适应硬件环境/单片机型号等
  6. //#include "delay.h"
  7. #include "systick.h"
  8. //定义
  9. #define I2C_DELAY 10
  10. const UINT8 BCD_decode_tab[0x10] = {
  11. 0x3F,//0011 1111B,0
  12. 0x06,//0000 0110B,1
  13. 0x5B,//0101 1011B,2
  14. 0x4F,//0100 1111B,3
  15. 0x66,//0110 0110B,4
  16. 0x6D,//0110 1101B,5
  17. 0x7D,//0111 1101B,6
  18. 0x27,//0010 0111B,7
  19. 0x7F,//0111 1111B,8
  20. 0x6F,//0110 1111B,9
  21. 0x77,//0011 0111B,A
  22. 0x7C,//0111 1100B,B
  23. 0x58,//0011 1001B,C
  24. 0x5E,//0101 1110B,D
  25. 0x79,//0111 1001B,E
  26. 0x71 //0111 0001B,F
  27. };//BCD
  28. const UINT8 Letter_tab[38] = {
  29. 0x3F,//0011 1111B,0
  30. 0x06,//0000 0110B,1
  31. 0x5B,//0101 1011B,2
  32. 0x4F,//0100 1111B,3
  33. 0x66,//0110 0110B,4
  34. 0x6D,//0110 1101B,5
  35. 0x7D,//0111 1101B,6
  36. 0x27,//0010 0111B,7
  37. 0x7F,//0111 1111B,8
  38. 0x6F,//0110 1111B,9
  39. 0x77,//0011 0111B,A
  40. 0x7C,//0111 1100B,B
  41. 0x58,//0011 1001B,C
  42. 0x5E,//0101 1110B,D
  43. 0x79,//0111 1001B,E
  44. 0x71,//0111 0001B,F
  45. 0x3D,//0011 1101B,G
  46. 0x76,//0111 0110B,H
  47. 0x10,//0001 0000B,I
  48. 0x0E,//0000 1110B,J
  49. 0x7A,//0111 1010B,K
  50. 0x38,//0011 1000B,L
  51. 0x55,//0101 0101B,M
  52. 0x54,//0101 0100B,N
  53. 0x5C,//0101 1100B,O
  54. 0x73,//0111 0011B,P
  55. 0x67,//0110 0111B,Q
  56. 0x50,//0101 0000B,R
  57. 0x65,//0110 0101B,S
  58. 0x78,//0111 1000B,T
  59. 0x3E,//0011 1110B,U
  60. 0x1C,//0001 1100B,V
  61. 0x6A,//0110 1010B,W
  62. 0x64,//0110 0100B,X
  63. 0x6E,//0110 1110B,Y
  64. 0x5A,//0101 1010B,Z
  65. 0x40,//0100 0000B,-
  66. 0x00//0000 0000B,灭
  67. };//BCD
  68. void CH455_Init(void)
  69. {
  70. GPIO_InitTypeDef GPIO_InitStructure;
  71. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE ); //使能GPIOB时钟
  72. //stm32f103中PB3、PB4、PA15为JTAG引脚,若要作为普通IO口来使用的话,要禁用JTAG功能。
  73. //RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  74. //GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
  75. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
  76. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //推挽输出
  77. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  78. GPIO_Init(GPIOB, &GPIO_InitStructure);
  79. GPIO_SetBits(GPIOB,GPIO_Pin_10|GPIO_Pin_11);
  80. CH455_Write( CH455_SYSON_8 ); //八段式
  81. EXTI_MY_INIT();
  82. }
  83. void CH455H_SCL_D_OUT(void)
  84. {
  85. GPIO_InitTypeDef GPIO_InitStructure;
  86. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  87. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD ; //推挽输出
  88. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  89. GPIO_Init(GPIOB, &GPIO_InitStructure);
  90. delay_us(I2C_DELAY);
  91. }
  92. void CH455H_SDA_D_OUT(void)
  93. {
  94. GPIO_InitTypeDef GPIO_InitStructure;
  95. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  96. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD ; //推挽输出
  97. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  98. GPIO_Init(GPIOB, &GPIO_InitStructure);
  99. delay_us(I2C_DELAY);
  100. }
  101. void CH455H_SDA_D_IN(void)
  102. {
  103. GPIO_InitTypeDef GPIO_InitStructure;
  104. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  105. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING ; //推挽输出
  106. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  107. GPIO_Init(GPIOB, &GPIO_InitStructure);
  108. delay_us(I2C_DELAY);
  109. }
  110. /*
  111. * 函数功能:IIC产生起始信号函数
  112. * 输入参数:无
  113. * 输出参数:无
  114. */
  115. void CH455_I2c_Start(void)
  116. {
  117. CH455_SDA_D_OUT;
  118. CH455_SCL = high; //SCL = high;
  119. delay_us(I2C_DELAY);
  120. CH455_SDA = high; //SDA = high;
  121. delay_us(I2C_DELAY);
  122. CH455_SDA = low; //SDA = low;
  123. delay_us(I2C_DELAY);
  124. CH455_SCL = low; //SCL = low;
  125. delay_us(I2C_DELAY);
  126. }
  127. /*
  128. * 函数功能:IIC产生停止信号
  129. * 输入参数:无
  130. * 输出参数:无
  131. */
  132. void CH455_I2c_Stop(void)
  133. {
  134. CH455_SDA_D_OUT;
  135. CH455_SCL = low; //SCL = low;
  136. delay_us(I2C_DELAY);
  137. CH455_SDA = low; //SDA = low;
  138. delay_us(I2C_DELAY);
  139. CH455_SCL = high; //SCL = high;
  140. delay_us(I2C_DELAY);
  141. CH455_SDA = high; //SDA = high;
  142. delay_us(I2C_DELAY);
  143. }
  144. UINT8 IIC_Wait_Ack(void)
  145. {
  146. UINT8 ucErrTime=0;
  147. CH455_SDA_D_IN; //SDA设置为输入
  148. //CH455_SDA=1;delay_us(1);
  149. CH455_SCL=0;delay_us(1);
  150. while(READ_SDA)
  151. {
  152. ucErrTime++;
  153. if(ucErrTime>250)
  154. {
  155. CH455_I2c_Stop();
  156. return 1;
  157. }
  158. delay_us(1);
  159. }
  160. //CH455_SCL=0;//时钟输出0
  161. return 0;
  162. }
  163. /*
  164. * 函数功能:IIC发送一个字节
  165. * 输入参数:一个字节
  166. * 输出参数:无
  167. */
  168. void CH455_I2c_WrByte(unsigned char IIC_Byte)
  169. {
  170. unsigned char i;
  171. CH455_SDA_D_OUT;
  172. for(i = 0; i < 8; i++)
  173. {
  174. if(IIC_Byte & 0x80)
  175. CH455_SDA = high; //SDA=high;
  176. else
  177. CH455_SDA = low; //SDA=low;
  178. delay_us(I2C_DELAY);
  179. CH455_SCL = high; //SCL=high;
  180. delay_us(I2C_DELAY);
  181. CH455_SCL = low; //SCL=low;
  182. delay_us(I2C_DELAY);
  183. IIC_Byte<<=1;
  184. }
  185. delay_us(I2C_DELAY);
  186. CH455_SDA = high; //SDA=1;
  187. delay_us(I2C_DELAY);
  188. CH455_SCL = high; //SCL=1;
  189. delay_us(I2C_DELAY);
  190. CH455_SCL = low; //SCL=0;
  191. delay_us(I2C_DELAY);
  192. }
  193. unsigned char CH455_I2c_RdByte( void ) //读一个字节数据
  194. {
  195. unsigned char i, bytedata;
  196. CH455_SDA_D_IN; //将数据设置为输入模式
  197. delay_us(I2C_DELAY);
  198. CH455_SDA = 1; //数据线拉高
  199. delay_us(I2C_DELAY);
  200. CH455_SCL = 0;
  201. delay_us(I2C_DELAY);
  202. for(i = 0; i < 8; i++) //读8位数据
  203. {
  204. CH455_SCL = 1;
  205. delay_us(I2C_DELAY);
  206. bytedata <<= 1;
  207. delay_us(I2C_DELAY);
  208. bytedata |= (CH455_SDA_IN);
  209. delay_us(I2C_DELAY);
  210. CH455_SCL = 0;
  211. delay_us(I2C_DELAY);
  212. }
  213. CH455_SDA_D_OUT; //数据线设置回输出模式
  214. CH455_SDA = high; //SDA=1;
  215. delay_us(I2C_DELAY);
  216. CH455_SCL = high; //SCL=1;
  217. delay_us(I2C_DELAY);
  218. CH455_SCL = low; //SCL=0;
  219. delay_us(I2C_DELAY);
  220. return(bytedata);//返回数据
  221. }
  222. void CH455_Write( UINT16 cmd ) //写命令
  223. {
  224. CH455_I2c_Start(); //启动总线
  225. CH455_I2c_WrByte(((UINT8)(cmd>>7)&CH455_I2C_MASK)|CH455_I2C_ADDR);
  226. CH455_I2c_WrByte((UINT8)cmd); //发送数据
  227. CH455_I2c_Stop(); //结束总线
  228. }
  229. UINT8 CH455_Read( void ) //读取按键
  230. {
  231. UINT8 keycode;
  232. CH455_I2c_Start(); //启动总线
  233. CH455_I2c_WrByte((UINT8)(CH455_GET_KEY>>7)&CH455_I2C_MASK|0x01|CH455_I2C_ADDR);
  234. keycode=CH455_I2c_RdByte(); //读取数据
  235. CH455_I2c_Stop(); //结束总线
  236. return keycode;
  237. }
  238. void EXTI_MY_INIT(void)
  239. {
  240. GPIO_InitTypeDef GPIO_InitStructure;
  241. EXTI_InitTypeDef EXTI_InitStructure;
  242. NVIC_InitTypeDef NVIC_InitStructure;
  243. //GPIO的复用功能必须打开
  244. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
  245. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  246. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_1;
  247. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  248. GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
  249. GPIO_Init(GPIOB, &GPIO_InitStructure);
  250. //配置外部中断源并进行中断源和GPIO的连接
  251. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource5);
  252. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
  253. EXTI_InitStructure.EXTI_Line =EXTI_Line5|EXTI_Line1;
  254. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  255. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  256. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  257. EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
  258. NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //使能按键所在的外部中断通道
  259. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级2,
  260. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //子优先级1
  261. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
  262. NVIC_Init(&NVIC_InitStructure);
  263. NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn; //使能按键所在的外部中断通道
  264. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级2,
  265. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; //子优先级1
  266. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
  267. NVIC_Init(&NVIC_InitStructure);
  268. }
  269. volatile UINT8 USER_BUTTON = 0;
  270. void EXTI1_IRQHandler(void)
  271. {
  272. USER_BUTTON = 1;
  273. EXTI_ClearITPendingBit(EXTI_Line1); //清除LINE11线路挂起位
  274. }
  275. void EXTI9_5_IRQHandler(void)
  276. {
  277. if(KEY3==0)
  278. {
  279. USER_BUTTON = 3;
  280. EXTI_ClearITPendingBit(EXTI_Line5); //清除LINE5线路挂起位
  281. }
  282. // if(KEY4==0)
  283. // {
  284. // USER_BUTTON = 4;
  285. // EXTI_ClearITPendingBit(EXTI_Line6); //清除LINE6线路挂起位
  286. // }
  287. }