VirtualCOM.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #include "includes.h"
  2. /*********************************************
  3. 使用说明:
  4. 1.首先把TIM_TX_Configuration(),TIM_RX_Configuration(),EXTIinit()中NVIC相关的配置配置好
  5. 2.添加中断函数TIM2_IRQHandler(),TIM3_IRQHandler(),EXTI15_10_IRQHandler()
  6. 3.根据当前主频和所需的波特率,配置执行VirtualCOM_Init(SYSCLK_FREQ_36M,BAUDRATE_9600)初始化函数,
  7. 然后就可以从定时器中断中获取接收到的数据,以及使用VirtualCOM_StringSend("abc\r\n")发送数据
  8. 备注:此方案用了3个定时器,其中TIM2的作为仅是做延迟,完全可以用COM_RX_Start_Delay(n)取合适的值代替,省一个定时器
  9. ***********************************************/
  10. unsigned char g_ucRecvStat = COM_STOP_BIT;
  11. unsigned char g_ucRecvData = 0;
  12. unsigned char g_iDelayCt=26;//当定时器配置最小时间为5us时,g_iDelayCt固定取26
  13. int iPrescaler =0;
  14. int iPeriod = 0;
  15. unsigned char iPeriodFix = 14;
  16. int g_iCOMRXDelayCt = 0;
  17. /************************************
  18. TIM_Delay_us
  19. *************************************/
  20. int g_iTIM_DelayUs_ct=0;
  21. void TIM_Delay_us(int timeus)//
  22. {
  23. g_iTIM_DelayUs_ct = timeus;
  24. TIM_Cmd(TIM2,ENABLE);
  25. while(g_iTIM_DelayUs_ct>0);
  26. TIM_Cmd(TIM2,DISABLE);
  27. }
  28. /************************************
  29. 配置定时器中断参数
  30. *************************************/
  31. void TIM_TX_Configuration()
  32. {
  33. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  34. // NVIC_InitTypeDef NVIC_InitStructure;//定时器中断向量配置已放在MY_NVIC_Init()中
  35. // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  36. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  37. TIM_DeInit(TIM2);
  38. TIM_InternalClockConfig(TIM2);
  39. TIM_TimeBaseStructure.TIM_Prescaler = iPrescaler - 1;
  40. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  41. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  42. TIM_TimeBaseStructure.TIM_Period = iPeriod - 1;
  43. TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
  44. TIM_ClearFlag(TIM2, TIM_FLAG_Update);
  45. TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
  46. TIM_Cmd(TIM2,DISABLE);
  47. // NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  48. // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
  49. // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  50. // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  51. // NVIC_Init(&NVIC_InitStructure);
  52. }
  53. void TIM_RX_Configuration()
  54. {
  55. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  56. // NVIC_InitTypeDef NVIC_InitStructure;//定时器中断向量配置已放在MY_NVIC_Init()中
  57. // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  58. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  59. TIM_DeInit(TIM3);
  60. TIM_InternalClockConfig(TIM3);
  61. TIM_TimeBaseStructure.TIM_Prescaler = iPrescaler - 1;
  62. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  63. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  64. TIM_TimeBaseStructure.TIM_Period = iPeriod*g_iDelayCt + iPeriodFix;
  65. TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
  66. TIM_ClearFlag(TIM3, TIM_FLAG_Update);
  67. TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
  68. TIM_Cmd(TIM3,DISABLE);
  69. // NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  70. // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
  71. // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  72. // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  73. // NVIC_Init(&NVIC_InitStructure);
  74. }
  75. void TIM_IDLE_TD_Configuration()//空闲保存数据定时器中断
  76. {
  77. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  78. // NVIC_InitTypeDef NVIC_InitStructure;//定时器中断向量配置已放在MY_NVIC_Init()中
  79. // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  80. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  81. TIM_DeInit(TIM4);
  82. TIM_InternalClockConfig(TIM4);
  83. TIM_TimeBaseStructure.TIM_Prescaler = iPrescaler - 1;
  84. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  85. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  86. TIM_TimeBaseStructure.TIM_Period = iPeriod*g_iDelayCt*10 - 1;
  87. TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
  88. TIM_ClearFlag(TIM4, TIM_FLAG_Update);
  89. TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
  90. TIM_Cmd(TIM4,DISABLE);
  91. // NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
  92. // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
  93. // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  94. // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  95. // NVIC_Init(&NVIC_InitStructure);
  96. }
  97. /************************************
  98. 配置外部中断
  99. *************************************/
  100. void EXTIinit(void)
  101. {
  102. GPIO_InitTypeDef GPIO_InitStructure;
  103. EXTI_InitTypeDef EXTI_InitStructure;
  104. // NVIC_InitTypeDef NVIC_InitStructure;//外部中断向量配置已放在MY_NVIC_Init()中
  105. // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  106. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO , ENABLE);
  107. GPIO_InitStructure.GPIO_Pin = COM_RX_PIN;
  108. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  109. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  110. GPIO_Init(COM_RX_PORT, &GPIO_InitStructure);
  111. GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource13);
  112. EXTI_InitStructure.EXTI_Line = EXTI_Line13;
  113. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  114. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  115. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  116. EXTI_Init(&EXTI_InitStructure);
  117. //外部中断
  118. // NVIC_InitStructure.NVIC_IRQChannel=EXTI15_10_IRQn;
  119. // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  120. // NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  121. // NVIC_Init(&NVIC_InitStructure);
  122. }
  123. /************************************
  124. 配置串口IO
  125. *************************************/
  126. void VirtualCOM_IOconfig()
  127. {
  128. GPIO_InitTypeDef GPIO_InitStructure;
  129. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
  130. GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);
  131. GPIO_InitStructure.GPIO_Pin = (COM_TX_PIN);
  132. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  133. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  134. GPIO_Init(COM_TX_PORT, &GPIO_InitStructure);
  135. GPIO_SetBits(COM_TX_PORT, COM_TX_PIN);
  136. // GPIO_InitStructure.GPIO_Pin = (COM_RX_PIN); //在EXTIinit()中配置
  137. // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  138. // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  139. // GPIO_Init(COM_RX_PORT, &GPIO_InitStructure);
  140. }
  141. /************************************
  142. 初始化模拟串口
  143. *************************************/
  144. void VirtualCOM_Init(SYSCLK_FREQ SysclkFreq,BAUDRATE BaudRate)
  145. {
  146. switch (SysclkFreq)
  147. {
  148. case (SYSCLK_FREQ_72M):
  149. if(BaudRate == BAUDRATE_9600) {iPrescaler=24;iPeriod=12;g_iCOMRXDelayCt=150;}
  150. if(BaudRate == BAUDRATE_4800) {iPrescaler=48;iPeriod=12;g_iCOMRXDelayCt=300;}
  151. if(BaudRate == BAUDRATE_2400) {iPrescaler=96;iPeriod=12;g_iCOMRXDelayCt=600;}
  152. if(BaudRate == BAUDRATE_1200) {iPrescaler=192;iPeriod=12;g_iCOMRXDelayCt=1200;}
  153. break;
  154. case (SYSCLK_FREQ_48M):
  155. if(BaudRate == BAUDRATE_9600) {iPrescaler=16;iPeriod=12;g_iCOMRXDelayCt=110;}
  156. if(BaudRate == BAUDRATE_4800) {iPrescaler=32;iPeriod=12;g_iCOMRXDelayCt=220;}
  157. if(BaudRate == BAUDRATE_2400) {iPrescaler=64;iPeriod=12;g_iCOMRXDelayCt=440;}
  158. if(BaudRate == BAUDRATE_1200) {iPrescaler=128;iPeriod=12;g_iCOMRXDelayCt=880;}
  159. break;
  160. case (SYSCLK_FREQ_36M):
  161. if(BaudRate == BAUDRATE_9600) {iPrescaler=12;iPeriod=12;g_iCOMRXDelayCt=80;}
  162. if(BaudRate == BAUDRATE_4800) {iPrescaler=24;iPeriod=12;g_iCOMRXDelayCt=160;}
  163. if(BaudRate == BAUDRATE_2400) {iPrescaler=48;iPeriod=12;g_iCOMRXDelayCt=320;}
  164. if(BaudRate == BAUDRATE_1200) {iPrescaler=96;iPeriod=12;g_iCOMRXDelayCt=640;}
  165. break;
  166. }
  167. VirtualCOM_IOconfig();
  168. TIM_TX_Configuration();
  169. TIM_RX_Configuration();
  170. TIM_IDLE_TD_Configuration();
  171. EXTIinit();
  172. }
  173. /************************************
  174. VirtualCOM_ByteSend
  175. 发送一个字节
  176. *************************************/
  177. void VirtualCOM_ByteSend(unsigned char val)
  178. {
  179. u8 i = 0;
  180. COM_DATA_LOW();
  181. TIM_Delay_us(g_iDelayCt);
  182. for(i = 0; i < 8; i++)
  183. {
  184. if(val & 0x01)
  185. COM_DATA_HIGH();
  186. else
  187. COM_DATA_LOW();
  188. TIM_Delay_us(g_iDelayCt);
  189. val >>= 1;
  190. }
  191. COM_DATA_HIGH();
  192. TIM_Delay_us(g_iDelayCt);
  193. }
  194. /************************************
  195. VirtualCOM_StringSend
  196. 发送字符串
  197. *************************************/
  198. void VirtualCOM_StringSend(u8 *str)
  199. {
  200. while(*str != 0)
  201. {
  202. VirtualCOM_ByteSend(*str);
  203. str++;
  204. }
  205. }
  206. /************************************
  207. COM_RX_Start_Delay
  208. 接收启动定时器延迟函数
  209. *************************************/
  210. void COM_RX_Start_Delay(int ct)
  211. {
  212. while(ct-->0){
  213. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  214. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  215. __nop();__nop();__nop();__nop();__nop();
  216. }
  217. }
  218. /**************************************************
  219. 以下是中断代码
  220. ***************************************************
  221. unsigned char g_ucRX_START=0;
  222. unsigned char g_ucRX_STOP=0;
  223. char RecvBuf[1024];
  224. int RecvBufCt=0;
  225. void TIM2_IRQHandler(void)
  226. {
  227. if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET){
  228. g_iTIM_DelayUs_ct--;
  229. if(g_iTIM_DelayUs_ct<=0)TIM_Cmd(TIM2,DISABLE);
  230. TIM_ClearITPendingBit(TIM2,TIM_FLAG_Update);
  231. }
  232. }
  233. void TIM3_IRQHandler(void)
  234. {
  235. if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET){
  236. g_ucRecvStat++;
  237. if(g_ucRecvStat == COM_STOP_BIT){
  238. TIM_Cmd(TIM3, DISABLE);
  239. TIM_ClearITPendingBit(TIM3,TIM_FLAG_Update);
  240. RecvBuf[RecvBufCt]=g_ucRecvData;
  241. RecvBufCt++;
  242. g_ucRX_STOP=1;
  243. TIM_Cmd(TIM4,ENABLE);
  244. return;
  245. }
  246. if(COM_RX_STAT){ //'1'
  247. g_ucRecvData |= (1 << (g_ucRecvStat - 1));
  248. }
  249. else{ //'0'
  250. g_ucRecvData &= ~(1 <<(g_ucRecvStat - 1));
  251. }
  252. TIM_ClearITPendingBit(TIM3,TIM_FLAG_Update);
  253. }
  254. }
  255. void TIM4_IRQHandler(void)
  256. {
  257. if(TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET){
  258. if(g_ucRX_START==1 && g_ucRX_STOP==1){
  259. g_ucRX_START=0;
  260. g_ucRX_STOP=0;
  261. // printf("%s\r\n",RecvBuf);//此处RecvBuf已接收完整,可以取出
  262. RecvBufCt=0;
  263. memset(RecvBuf,0,sizeof(RecvBuf));
  264. TIM_Cmd(TIM4,DISABLE);
  265. }
  266. TIM_ClearITPendingBit(TIM4,TIM_FLAG_Update);
  267. }
  268. }
  269. void EXTI15_10_IRQHandler(void)
  270. {
  271. if(EXTI_GetITStatus(EXTI_Line13)!=RESET)
  272. {
  273. if(!COM_RX_STAT)
  274. {
  275. if(g_ucRecvStat == COM_STOP_BIT)
  276. {
  277. g_ucRecvStat = COM_START_BIT;
  278. COM_RX_Start_Delay(g_iCOMRXDelayCt);
  279. TIM_Cmd(TIM3, ENABLE);
  280. g_ucRX_START=1;
  281. g_ucRX_STOP=0;
  282. TIM_Cmd(TIM4,DISABLE);
  283. }
  284. }
  285. EXTI_ClearITPendingBit(EXTI_Line13);
  286. }
  287. }
  288. ***************************************************
  289. end
  290. ***************************************************/