这是STM8s003 外部中断的简单例程,当有外部中断时 串口输出客户需要的 十六进制数据。
单片机源程序如下:
- /* Includes ------------------------------------------------------------------*/
- #include "stm8s.h"
- #include "LED.h"
- /* Private defines -----------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- #define IWDG_500MS_REST (uint8_t)250
- //#define IWDG_400MS_REST (uint8_t)200
- //#define IWDG_300MS_REST (uint8_t)150
- //#define IWDG_250MS_REST (uint8_t)125
- //#define IWDG_200MS_REST (uint8_t)100
- //#define IWDG_100MS_REST (uint8_t)50
- //#define IWDG_50MS_REST (uint8_t)25
- //#define IWDG_4MS_REST (uint8_t)2
- //void Send(uint8_t dat);
- uint8_t cmd[132]={0x26,0x26,0x84,0x00,0x0B,0x40,0x26,0x01,0x31,0x32,0x33,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x31,0x32,0x33,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x35,0x30,0x35,0x32,
- 0x35,0x31,0x39,0x38,0x32,0x31,0x32,0x30,0x33,0x33,0x35,0x31,0x39,0x07,0xE4,0x0A,0x09,0x29,
- 0x0B,0x27,0x15,0x39,0x2C,0x00,0x00,0x02,0x31,0x32,0x33,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0xC8,0x00,0x0D,0x0A};
- void Delay(uint16_t nCount)
- {
- /* Decrement nCount value */
- while (nCount != 0)
- {
- nCount--;
- }
- }
- void Init_UART1(void)
- {
- UART1_DeInit();
- UART1_Init((u32)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
-
- UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
- // UART1_Cmd(ENABLE);
- }
- void Send(uint8_t dat)
- {
- while(( UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET));
-
- UART1_SendData8(dat);
-
- }
- void send_cmd(uint8_t *cmd,uint8_t len)
- { uint8_t i;
- for(i=0;i<len;i++)
- {
- Send(cmd[i]);
- }
- }
- void main(void)
- {
- uint8_t i=0;
- /* Initialize I/Os in Output Mode */
- CLK_DeInit();
- CLK_HSICmd(ENABLE);//启用内部高速振荡器(HSI?16MHz)?
- CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);//主时钟=16MHZ/1=16MHz?
- CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);//CPU时钟=主时钟/1=18MHZ
-
- GPIO_Init(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS, GPIO_MODE_OUT_PP_LOW_FAST);
- GPIO_Init(KEY_GPIO_PORT, (GPIO_Pin_TypeDef)KEY_GPIO_PINS, GPIO_MODE_IN_PU_IT );
-
- EXTI_SetExtIntSensitivity((EXTI_Port_TypeDef)EXTI_PORT_GPIOD, EXTI_SENSITIVITY_FALL_LOW);
- ITC_SetSoftwarePriority(ITC_IRQ_PORTD, ITC_PRIORITYLEVEL_2); //开启中断,中断优先级设置为高 button = higher sw priority
- // GPIO_WriteHigh(KEY_GPIO_PORT, KEY_GPIO_PINS);
- //CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
- Init_UART1();
- enableInterrupts();
- Delay(0xffff);
- Delay(0xffff);
- Delay(0xffff);
- Delay(0xffff);
- Delay(0xffff);
- Delay(0xffff);
- Delay(0xffff);
- Delay(0xffff);
- Delay(0xffff);
- Delay(0xffff);
- //enableInterrupts();
- //enableInterrupts();
- //Init_UART1();
- IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);//同意操作独立看门狗
- IWDG_SetPrescaler(IWDG_Prescaler_128);//看门狗预分频
- IWDG_SetReload(IWDG_500MS_REST);//设置看门狗复位时间
- IWDG_Enable();//启动独立看门狗
- //send_cmd(cmd,132);
- //enableInterrupts();
-
- /* Infinite loop */
- while (1)
- {
- GPIO_WriteHigh(LED_GPIO_PORT, LED_GPIO_PINS);
- IWDG_ReloadCounter();//看门狗复位 正常使用时不想发生看门狗复位,就运行
- // GPIO_WriteHigh(KEY_GPIO_PORT, KEY_GPIO_PINS);
- /*
- if(((GPIOD->IDR & (uint8_t)KEY_GPIO_PINS) ? SET : RESET)==RESET)
- {
- GPIO_WriteLow(LED_GPIO_PORT, LED_GPIO_PINS);
-
- Delay(0xff);
- if(((GPIOD->IDR & (uint8_t)KEY_GPIO_PINS) ? SET : RESET)==RESET)
- {
- send_cmd(cmd,132);
-
- }
- Delay(0xffff);
- Delay(0xffff);
-
- }
- //GPIO_WriteHigh(KEY_GPIO_PORT, KEY_GPIO_PINS);
- */
-
- }
-
-
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval : None
- */
- void assert_failed(u8* file, u32 line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
- /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
复制代码
所有资料51hei提供下载:
UARTR.7z
(121.9 KB, 下载次数: 38)
|