系列文章
提示:单片机与接口技术专栏,观看更多内容! 点我直达–>单片机与接口技术专栏
前言
1.内容一:要求利用定时器1模模式及中断函数法,精确控制LED3每隔t=(500+120)ms 切换亮灭状态,即亮 t ms → 暗t ms → 亮 t ms → 暗 t ms → 亮 t ms,其中***表示学号最后三位。要求给出源代码及注释。 选做1:利用定时器3实现上述效果。
1.main.c
#include <ioCC2530.h>
#include "sys_init.h"
uint counter = 0;
void main()
{
xtal_init();
led_init();
time1_init();
while(1)
{
if(counter>=620){
counter=0;
D6 = !D6;
}
}
}
#pragma vector = T1_VECTOR
__interrupt void T1_ISR(void)
{
EA=0;
counter=counter+1;
T1IF=0;
EA=1;
}
2.sys_init.c
#include <ioCC2530.h>
#include "sys_init.h"
void xtal_init(void)
{
SLEEPCMD &= ~0x04;
while(!(CLKCONSTA & 0x40));
CLKCONCMD &= ~0x47;
SLEEPCMD |= 0x04;
}
void led_init(void)
{
P1SEL &= ~0x01;
P1DIR |= 0x01;
D6 = 1;
}
void time1_init()
{
int value=125;
T1CTL = 0x0E;
T1CCTL0 |= 0x04;
T1CC0H = value/256;
T1CC0L= value %256;
IEN1|=0X02;
EA=1;
}
void halWait(unsigned char wait)
{
unsigned long largeWait;
if(wait == 0)
{return;}
largeWait = ((unsigned short) (wait << 7));
largeWait += 114*wait;
largeWait = (largeWait >> CLKSPD);
while(largeWait--);
return;
}
void MyWait(uint ms )
{
int n= ms%250;
int m=ms/250;
for(int i=0;i<m;i++){
halWait(250);
}
halWait(n);
}
3.sys_init.h
#include <ioCC2530.h>
#define D6 P1_0
#define uint unsigned int
#define uchar unsigned char
#define CLKSPD ( CLKCONCMD & 0x07 )
void xtal_init(void);
void led_init(void);
void time1_init(void);
void halWait(unsigned char wait);
总结
大家喜欢的话,给个👍,点个关注!继续跟大家分享敲代码过程中遇到的问题!
|