继续发一个也是定时器4的应用程序,这次是基于中断的方法的。实现功能跟上面软件查询的一样,简单的流水灯。
#include "STM8S105C_S.h"
#define uint unsigned int
#define uchar unsigned char
uchar table[]={0x00,0x01,0x04,0x08};
uchar i=0;
void GPIO_Init(void)
{
PD_DDR|=0x0d; //3个LED端口(PD3 PD2 PD0 )设为推挽输出,频率10M 方向,方式,频率
PD_CR1|=0x0d;
PD_CR2|=0x0d;
}
void Time4_Init(void)
{
TIM4_IER=0x01; //禁止触发中断,使能更新中断
TIM4_EGR=0x01; //计数器溢出时,更新事件产生
TIM4_PSCR=0x07; //对计数脉冲进行128分频
TIM4_ARR=255; //计数值,当计数器计数到这个值就溢出
TIM4_CR1=0x01; //使能计数器
_asm("rim");//这是开中断
}
main()
{
GPIO_Init();
Time4_Init();
while (1)
{
PD_ODR=table[i]; //往IO口写数据
}
}
@far @interrupt void TIM4_UPD_OVF_IRQHanlder (void)
{
TIM4_SR=0x00;
i++;
if(i==4)i=0;
}
/* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
* Copyright (c) 2007 STMicroelectronics
*/
typedef void @far (*interrupt_handler_t)(void);
struct interrupt_vector {
unsigned char interrupt_instruction;
interrupt_handler_t interrupt_handler;
};
@far @interrupt void NonHandledInterrupt (void)
{
/* in order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction
*/
return;
}
extern void _stext(); /* startup routine */
extern @far @interrupt void TIM4_UPD_OVF_IRQHanlder (void);
struct interrupt_vector const _vectab[] = {
{0x82, (interrupt_handler_t)_stext}, /* reset */
{0x82, NonHandledInterrupt}, /* trap */
{0x82, NonHandledInterrupt}, /* irq0 */
{0x82, NonHandledInterrupt}, /* irq1 */
{0x82, NonHandledInterrupt}, /* irq2 */
{0x82, NonHandledInterrupt}, /* irq3 */
{0x82, NonHandledInterrupt}, /* irq4 */
{0x82, NonHandledInterrupt}, /* irq5 */
{0x82, NonHandledInterrupt}, /* irq6 */
{0x82, NonHandledInterrupt}, /* irq7 */
{0x82, NonHandledInterrupt}, /* irq8 */
{0x82, NonHandledInterrupt}, /* irq9 */
{0x82, NonHandledInterrupt}, /* irq10 */
{0x82, NonHandledInterrupt}, /* irq11 */
{0x82, NonHandledInterrupt}, /* irq12 */
{0x82, NonHandledInterrupt}, /* irq13 */
{0x82, NonHandledInterrupt}, /* irq14 */
{0x82, NonHandledInterrupt}, /* irq15 */
{0x82, NonHandledInterrupt}, /* irq16 */
{0x82, NonHandledInterrupt}, /* irq17 */
{0x82, NonHandledInterrupt}, /* irq18 */
{0x82, NonHandledInterrupt}, /* irq19 */
{0x82, NonHandledInterrupt}, /* irq20 */
{0x82, NonHandledInterrupt}, /* irq21 */
{0x82, NonHandledInterrupt}, /* irq22 */
{0x82, TIM4_UPD_OVF_IRQHanlder}, /* irq23 */
{0x82, NonHandledInterrupt}, /* irq24 */
{0x82, NonHandledInterrupt}, /* irq25 */
{0x82, NonHandledInterrupt}, /* irq26 */
{0x82, NonHandledInterrupt}, /* irq27 */
{0x82, NonHandledInterrupt}, /* irq28 */
{0x82, NonHandledInterrupt}, /* irq29 */
}; |