求stc单片机的PCA定时器程序!!!

求stc单片机的PCA定时器程序!!!,第1张

/* --- 演示 STC 1T 系列单片机 用PCA功能实现16位定时器 --------*/

#include "reg51.h"

#include "intrins.h"

#define FOSC 18432000L

#define T100Hz (FOSC / 12 / 100)

typedef unsigned char BYTE

typedef unsigned int WORD

/*Declare SFR associated with the PCA */

sfr CCON = 0xD8//PCA control register

sbit CCF0 = CCON^0//PCA module-0 interrupt flag

sbit CCF1 = CCON^1//PCA module-1 interrupt flag

sbit CR = CCON^6//PCA timer run control bit

sbit CF = CCON^7//PCA timer overflow flag

sfr CMOD = 0xD9//PCA mode register

sfr CL = 0xE9//PCA base timer LOW

sfr CH = 0xF9//PCA base timer HIGH

sfr CCAPM0 = 0xDA//PCA module-0 mode register

sfr CCAP0L = 0xEA//PCA module-0 capture register LOW

sfr CCAP0H = 0xFA//PCA module-0 capture register HIGH

sfr CCAPM1 = 0xDB//PCA module-1 mode register

sfr CCAP1L = 0xEB//PCA module-1 capture register LOW

sfr CCAP1H = 0xFB//PCA module-1 capture register HIGH

sfr PCAPWM0 = 0xf2

sfr PCAPWM1 = 0xf3

sbit PCA_LED = P1^0//PCA test LED

BYTE cnt

WORD value

void PCA_isr() interrupt 7 using 1

{

CCF0 = 0//Clear interrupt flag

CCAP0L = value

CCAP0H = value >>8//Update compare value

value += T100Hz

if (cnt-- == 0)

{

cnt = 100//Count 100 times

PCA_LED = !PCA_LED//Flash once per second

}

}

void main()

{

CCON = 0//Initial PCA control register

//PCA timer stop running

//Clear CF flag

//Clear all module interrupt flag

CL = 0//Reset PCA base timer

CH = 0

CMOD = 0x00//Set PCA timer clock source as Fosc/12

//Disable PCA timer overflow interrupt

value = T100Hz

CCAP0L = value

CCAP0H = value >>8//Initial PCA module-0

value += T100Hz

CCAPM0 = 0x49//PCA module-0 work in 16-bit timer mode

//and enable PCA interrupt

CR = 1//PCA timer start run

EA = 1

cnt = 0

while (1)

}

#include<reg52.h>

unsigned char i

main()

{

P2=0xff

TMOD=0X01 //一般用方式1

EA=1

ET0=1

//IE=0xff //多余

TH0=0

TL0=0

TR0=1

while(1)

}

void time0() interrupt 1 //定时器0

{

TH0=0

TL0=0//重新装初值

P2--

}

以上程序中断会每隔65536us(如果你眼睛够快的话,但是以这个速度人眼应该感觉不到,你可以每隔20次中断再让P2自减一次,这样效果更好)发生一次,但是由于P2--自减8次之后就会变成0x00,所以之后就看不到任何现象,如果在P2之后添上如下语句就会一直看到所对应的现象(比如流水灯):

i++

if(i==8)

{

i=0

P2=0xff

}

玩单片机一定要多练,希望对你有帮助,再不懂就给我留言,祝你成功

既然你选择的STC单片机带PCA,为什么不直接利用PCA模块配置成PWM功能,说一下具体型号,给你段利用PWM产生固定频率方波的代码参考。

//****************pwm.h********************************

#define Start_PWM() CR = 1 //PCA计时器开始工作

#define Stop_PWM() CR = 0 //PCA计时器停止工作

extern void InitPWM()

extern void ChangePWM(uint16 f)

//****************pwm.C********************************

#include "pwm.h"

#include "stc15F2k60s2.h"

#define CCP_S0 0x10 //P_SW1.4

#define CCP_S1 0x20 //P_SW1.5

static uint16 data frequency

static uint16 data value

void PCAisr() interrupt 7

{

CCF1 = 0 //清中断标志

CCAP1L = value

CCAP1H = value >>8 //更新比较值

value += frequency //累加

}

void SetPWMFrenquency(uint16 frq)

{

frequency=frq

value = frequency

CCAP1L = value //将值得低8位赋给CCAP1L

CCAP1H = value >>8 //高8位赋给CCAP1H

value += frequency //累加,为下一次赋值做好准备。产生同固定频率的方波就靠它了。

}

//p2.6/CCP1_3

void InitPWM()

{

ACC = P_SW1

ACC &= ~(CCP_S0 | CCP_S1) //CCP_S0=0 CCP_S1=1

ACC |= CCP_S1 //(P2.4/ECI_3, P2.5/CCP0_3, P2.6/CCP1_3, P2.7/CCP2_3)

P_SW1 = ACC

CCON = 0//初始化PCA控制寄存器

//PCA定时器停止

//清除CF标志

//清除模块中断标志

CL = CH = 0 //复位PCA计数寄存器

CMOD = 0x08 //设置PCA时钟源为sysclock,禁止PCA计数器溢出中断

CCAPM1 = 0x4d//0100 1101//PCA模块1为16位定时器模式,同时反转(P2.6)口

PPCA = 1 //中断优先级

SetPWMFrenquency(12000) //frq=24000000/2/1000=12000,每秒钟1000hz

}

void ChangePWM(uint16 frq)

{

Stop_PWM() //停止PCA计时器

CL=CH=0 //PCA计数器清零

CCF1=0 //PCA1的中断标记清零

SetPWMFrenquency(frq)

Start_PWM()

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7784327.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-09
下一篇 2023-04-09

发表评论

登录后才能评论

评论列表(0条)

保存