本帖最后由 solarmax1969 于 2012-8-22 10:41 编辑
老Xuan是初学者,这个是第一个程序,想必菜鸟也能用到。
一些经验,小技巧贡献出来,希望大家一起提高!
程序目的:单片机:STM8L151K4T6,TIM4 ,1秒
/***********************************************************
**文 件 名:sys.c
**创建时间:2012/08/10
**作 者:JIM XUAN
************************************************************
**功 能: 处理单片机核心资源的函数
**备 注: 采用内置的RC=16Mhz振荡器 ,默认8分频,SysClk=2mHZ
**修 改:
************************************************************
*/
#include <STM8L151K4.h>
#include "SM_STM8_ALL.H"
#include "sys.h"
// 系统实际工作在 2 Mhz clk 。可以分频系数有128,64,32,16,8,4,2,1 (值7..0)
// 2M clk,先经 16 分频,得到计数器clk = 1/2 ns *16 = 8ns
// 2ms需要计数次数= 2000/8 = 250
#define __BASE_CNT_MAX 250 //约2ms 中断一次
void TIM4_Init(void)
{
TIM4_CNTR = 0;
TIM4_PSCR = 4; //16分频
TIM4_ARR = __BASE_CNT_MAX ;
TIM4_EGR = 0x01;
TIM4_IER = 0x01;//使能溢出中断
TIM4_CR1 = 0x11 ;//开启定时器
}
@far @interrupt void TIM4_OVF(void) //TIM4定时器溢出中断
{
static usint OneSnd_Cnt = 0;
CLI();
// 1000ms 定时
if (OneSnd_Cnt >= (500-1)) //1000 ms 到
{
OneSnd_Cnt = 0;
gOneSecond = 0xff;
}
else
OneSnd_Cnt++ ;
TIM4_SR1 =0; //清标志位,以免重复进入。
SEI();
return;
}
void Port_Init(void)
{
//方向->输出=1,推挽=1;
//Port A
//Port B
PB_DDR |= (1<<D3) | (1<<D4);
PB_CR1 |= (1<<D3) | (1<<D4);//高、低大电流的推挽输出
PB_CR2 &= ~(1<<D3) ; //推挽输出
PB_CR2 &= ~(1<<D4) ; //推挽输出
//Port D
PD_DDR |= (1<<D0) | (1<<D3);
PD_CR1 |= (1<<D0) | (1<<D3);//高、低大电流的推挽输出
PD_CR2 &= ~(1<<D0) ; //推挽输出
PD_CR2 &= ~(1<<D3) ; //推挽输出
}
void Clock_Init(void)
{
CLK_CKDIVR = 0x03; //8分频
CLK_ICKCR = 0x11; //启用内部RC=16mhz
while((CLK_ICKCR & 0x02)==0); //等待时钟稳定
CLK_PCKENR1 = 0xff; //定义,时钟提供给设备
CLK_PCKENR2 |= 0b00111111; //定义,时钟提供给设备
}
void Sys_Init(void)
{
CLI();
Clock_Init(); //妈妈的,这款单片机为了低功耗,需选择时钟是否对设备提供。很关键,否则内部设备不工作的!
Port_Init();
TIM4_Init();
SEI ();
}
-----------------------------------
中断函数如下:stm8_interrupt_vector.c
/* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
* Copyright (c) 2007 STMicroelectronics
* File : STM8_INTERRUPT_VECTOR.C
*/
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_OVF(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, NonHandledInterrupt}, /* irq23 */
{0x82, NonHandledInterrupt}, /* irq24 */
{0x82, TIM4_OVF}, /* irq25 */
{0x82, NonHandledInterrupt}, /* irq26 */
{0x82, NonHandledInterrupt}, /* irq27 */
{0x82, NonHandledInterrupt}, /* irq28 */
{0x82, NonHandledInterrupt}, /* irq29 */
};
----------------------------
sys.h 文件如下:
/***********************************************************
**文 件 名:sys.h
**创建时间:2012/08/10
**作 者:JIM XUAN
************************************************************
**功 能:处理单片机核心资源的定义
**备 注:
**修 改:
***********************************************************/
#ifndef __sys_h
#define __sys_h
// ******************** 头引入******************************
#include "STM8L151K4.h"
// ****************** 全局变量的定义引用 *******************
extern uchar gOneSecond ;
#define D0 0
#define uchar unsigned char
#define usint unsigned short int
#define ulint unsigned long int
#define SEI() _asm("rim") // 允许CPU 全局中断
#define CLI() _asm("sim") // 禁止CPU 全局中断
#define NOP() _asm("NOP")
// ****************** LED 指示 *******************
#define LED4_OFF PD_ODR |= (1<<D0)
#define LED4_ON PD_ODR &= ~(1<<D0)
#define LED4_FLASH PD_ODR ^= (1<<D0)
void Sys_Init(void);
#endif
//交流可加QQ=540072586 |
|