找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1575|回复: 1
打印 上一主题 下一主题
收起左侧

stm8s003单片机外部中断和串口例程 iar工程文件

[复制链接]
跳转到指定楼层
楼主
ID:800716 发表于 2021-1-14 11:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这是STM8s003 外部中断的简单例程,当有外部中断时 串口输出客户需要的 十六进制数据。

单片机源程序如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm8s.h"
  3. #include "LED.h"

  4. /* Private defines -----------------------------------------------------------*/
  5. /* Private function prototypes -----------------------------------------------*/
  6. /* Private functions ---------------------------------------------------------*/
  7. #define  IWDG_500MS_REST  (uint8_t)250
  8. //#define  IWDG_400MS_REST  (uint8_t)200
  9. //#define  IWDG_300MS_REST  (uint8_t)150
  10. //#define  IWDG_250MS_REST  (uint8_t)125
  11. //#define  IWDG_200MS_REST  (uint8_t)100
  12. //#define  IWDG_100MS_REST  (uint8_t)50
  13. //#define  IWDG_50MS_REST   (uint8_t)25
  14. //#define  IWDG_4MS_REST    (uint8_t)2
  15. //void Send(uint8_t dat);
  16. uint8_t cmd[132]={0x26,0x26,0x84,0x00,0x0B,0x40,0x26,0x01,0x31,0x32,0x33,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  17.                   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  18.                   0x00,0x00,0x00,0x00,0x31,0x32,0x33,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x35,0x30,0x35,0x32,
  19.                   0x35,0x31,0x39,0x38,0x32,0x31,0x32,0x30,0x33,0x33,0x35,0x31,0x39,0x07,0xE4,0x0A,0x09,0x29,
  20.                   0x0B,0x27,0x15,0x39,0x2C,0x00,0x00,0x02,0x31,0x32,0x33,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  21.                   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22.                   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23.                   0x00,0x00,0xC8,0x00,0x0D,0x0A};


  24. void Delay(uint16_t nCount)
  25. {
  26.   /* Decrement nCount value */
  27.   while (nCount != 0)
  28.   {   
  29.     nCount--;
  30.   }
  31. }

  32. void Init_UART1(void)
  33. {
  34.         UART1_DeInit();
  35.         UART1_Init((u32)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
  36.         
  37.         UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);

  38. //        UART1_Cmd(ENABLE);
  39. }
  40. void Send(uint8_t dat)
  41. {
  42.   while(( UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET));
  43.         
  44.                 UART1_SendData8(dat);
  45.         
  46. }
  47. void send_cmd(uint8_t *cmd,uint8_t len)
  48. { uint8_t i;
  49.   for(i=0;i<len;i++)
  50.   {
  51.     Send(cmd[i]);
  52.   }

  53. }


  54. void main(void)
  55. {
  56. uint8_t i=0;
  57.                 /* Initialize I/Os in Output Mode */
  58.                 CLK_DeInit();
  59.                 CLK_HSICmd(ENABLE);//启用内部高速振荡器(HSI?16MHz)?
  60.                 CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);//主时钟=16MHZ/1=16MHz?
  61.                 CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);//CPU时钟=主时钟/1=18MHZ
  62.                
  63.                 GPIO_Init(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS, GPIO_MODE_OUT_PP_LOW_FAST);
  64.                 GPIO_Init(KEY_GPIO_PORT, (GPIO_Pin_TypeDef)KEY_GPIO_PINS, GPIO_MODE_IN_PU_IT );
  65.                  
  66.                 EXTI_SetExtIntSensitivity((EXTI_Port_TypeDef)EXTI_PORT_GPIOD, EXTI_SENSITIVITY_FALL_LOW);
  67.                 ITC_SetSoftwarePriority(ITC_IRQ_PORTD, ITC_PRIORITYLEVEL_2); //开启中断,中断优先级设置为高 button = higher sw priority
  68.                // GPIO_WriteHigh(KEY_GPIO_PORT, KEY_GPIO_PINS);
  69.                 //CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
  70.                 Init_UART1();
  71.                 enableInterrupts();
  72.                 Delay(0xffff);
  73.                 Delay(0xffff);
  74.                 Delay(0xffff);
  75.                  Delay(0xffff);
  76.                   Delay(0xffff);
  77.                    Delay(0xffff);
  78.                     Delay(0xffff);
  79.                     Delay(0xffff);
  80.                     Delay(0xffff);

  81.                     Delay(0xffff);
  82.                 //enableInterrupts();
  83.                     //enableInterrupts();
  84.                     //Init_UART1();
  85.                 IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);//同意操作独立看门狗
  86.                 IWDG_SetPrescaler(IWDG_Prescaler_128);//看门狗预分频
  87.                 IWDG_SetReload(IWDG_500MS_REST);//设置看门狗复位时间
  88.                 IWDG_Enable();//启动独立看门狗
  89.                 //send_cmd(cmd,132);
  90.                 //enableInterrupts();
  91.                
  92. /* Infinite loop */
  93.   while (1)
  94.   {
  95.     GPIO_WriteHigh(LED_GPIO_PORT, LED_GPIO_PINS);
  96.     IWDG_ReloadCounter();//看门狗复位 正常使用时不想发生看门狗复位,就运行
  97.    // GPIO_WriteHigh(KEY_GPIO_PORT, KEY_GPIO_PINS);
  98.     /*
  99.     if(((GPIOD->IDR & (uint8_t)KEY_GPIO_PINS) ? SET : RESET)==RESET)
  100.     {
  101.       GPIO_WriteLow(LED_GPIO_PORT, LED_GPIO_PINS);
  102.       
  103.       Delay(0xff);
  104.       if(((GPIOD->IDR & (uint8_t)KEY_GPIO_PINS) ? SET : RESET)==RESET)
  105.       {
  106.         send_cmd(cmd,132);
  107.         
  108.       }
  109.       Delay(0xffff);
  110.       Delay(0xffff);
  111.      
  112.     }
  113.     //GPIO_WriteHigh(KEY_GPIO_PORT, KEY_GPIO_PINS);
  114.     */
  115.         
  116.   }
  117.   
  118.   
  119. }

  120. #ifdef USE_FULL_ASSERT

  121. /**
  122.   * @brief  Reports the name of the source file and the source line number
  123.   *   where the assert_param error has occurred.
  124.   * @param file: pointer to the source file name
  125.   * @param line: assert_param error line source number
  126.   * @retval : None
  127.   */
  128. void assert_failed(u8* file, u32 line)
  129. {
  130.   /* User can add his own implementation to report the file name and line number,
  131.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  132.   /* Infinite loop */
  133.   while (1)
  134.   {
  135.   }
  136. }
  137. #endif

  138. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
UARTR.7z (121.9 KB, 下载次数: 36)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:81138 发表于 2021-1-25 23:20 来自手机 | 只看该作者
沙发吖
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表