C8051F120单片机产生PWM 10
externunsignedintCEX0_Compare_Value;voidpca_init(void){chardataSFRPAGE_SAVE=SFRPAGE;/...
extern unsigned int CEX0_Compare_Value;
void pca_init (void)
{
char data SFRPAGE_SAVE = SFRPAGE; // preserve SFRPAGE
SFRPAGE =SFR_PAGE_0;
PCA0CN = 0x00; //PCA定时器使能
PCA0MD = 0x08; // Use SYSCLK as time base
PCA0CPM0 = 0xC2; //使能16位PWM
//PCA0L = 0x00;/*定时/计数器初值为0000H*/
//PCA0H = 0x00;
CEX0_Compare_Value = 65536 - 65536;
PCA0CPL0 = (CEX0_Compare_Value & 0x00FF);
PCA0CPH0 = (CEX0_Compare_Value & 0xFF00)>>8;
SFRPAGE = SFRPAGE_SAVE; // restore SFRPAGE
}
void Port_IO_Init()
{
SFRPAGE = CONFIG_PAGE;
XBR0 = 0x0c;//使能了PCA的 CEX0口,在P0.4产生PWM P0.0 P0.1接UART0的TX RX
XBR1 = 0x00;
XBR2 = 0x44;//交叉开关使能,P0.2 P0.3接UART1的TX RX
P0MDOUT = 0x15;
}
void Oscillator_Init()
{
SFRPAGE = CONFIG_PAGE;
OSCICN = 0x83;
}
就是不能出波形,P0.4只在高电平显示 展开
void pca_init (void)
{
char data SFRPAGE_SAVE = SFRPAGE; // preserve SFRPAGE
SFRPAGE =SFR_PAGE_0;
PCA0CN = 0x00; //PCA定时器使能
PCA0MD = 0x08; // Use SYSCLK as time base
PCA0CPM0 = 0xC2; //使能16位PWM
//PCA0L = 0x00;/*定时/计数器初值为0000H*/
//PCA0H = 0x00;
CEX0_Compare_Value = 65536 - 65536;
PCA0CPL0 = (CEX0_Compare_Value & 0x00FF);
PCA0CPH0 = (CEX0_Compare_Value & 0xFF00)>>8;
SFRPAGE = SFRPAGE_SAVE; // restore SFRPAGE
}
void Port_IO_Init()
{
SFRPAGE = CONFIG_PAGE;
XBR0 = 0x0c;//使能了PCA的 CEX0口,在P0.4产生PWM P0.0 P0.1接UART0的TX RX
XBR1 = 0x00;
XBR2 = 0x44;//交叉开关使能,P0.2 P0.3接UART1的TX RX
P0MDOUT = 0x15;
}
void Oscillator_Init()
{
SFRPAGE = CONFIG_PAGE;
OSCICN = 0x83;
}
就是不能出波形,P0.4只在高电平显示 展开
3个回答
展开全部
C8051F120单片机产生PWM程序为:
olf2010-6-21 22:421#
//PWM输出脚P0.3
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include // SFR declarations
#include
#include
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void PORT_Init (void);
void PCA_Init (void);
void PCA_ISR (void);
void SYSCLK_Init (void);
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
unsigned long b;
WDTCN = 0xde; // disable watchdog timer
WDTCN = 0xad;
SYSCLK_Init ();
PORT_Init (); /*初始化I/O口*/
PCA_Init (); /游扒*PCA初始化成8位脉宽调置方式*/
EIE1= 0x08;
EA=1;
PCA0CPH0=0x20;
while (1)
{ }
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
XBR0 = 0x11; // XBAR0: Initial Reset Value
XBR1 = 0x00; // XBAR1: Initial Reset Value
毕磨巧 XBR2 = 0x40; // XBAR2: Initial Reset Value
// Port configuration (1 = Push Pull Output)
PRT0CF = 0x08; // Output configuration for P0
PRT1CF = 0x00; // Output configuration for P1
PRT2CF = 0x00; // Output configuration for P2
PRT3CF = 0x00; // Output configuration for P3
手键 }
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
void SYSCLK_Init (void)
{
unsigned int i; // delaynms counter
OSCXCN = 0x67; // start external oscillator with
for (i=0; i < 256; i++) ; // XTLVLD blanking interval (>1ms)
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x88; // select external oscillator as SYSCLK
}
//-----------------------------------------------------------------------------
// PCA_Init
//-----------------------------------------------------------------------------
void PCA_Init (void)
{
// PCA0CPM0 = 0x42; // PCA Capture/Compare Register 0
PCA0CN = 0x40;
PCA0CPM1 = 0x42;
PCA0CPL0 = 0x00;
PCA0CPH0 = 0x00;
// PCA0MD &= 0x40;
PCA0MD = 0x01;
}
//-----------------------------------------------------------------------------
// PCA_ISR
//-----------------------------------------------------------------------------
void PCA_ISR (void) interrupt 9
{ unsigned char a ;
bit PWM_PAC;
CF = 0;
if(PWM_PAC==0) //占空比由0 >>0xfe
{
PCA0CPH1=a;
a++;
if(a==0xfe){PWM_PAC=1;}
}
else //占空比由0xfe >>0
{
PCA0CPH1=a;
a--;
if(a==0){PWM_PAC=0;}
}
}
olf2010-6-21 22:421#
//PWM输出脚P0.3
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include // SFR declarations
#include
#include
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void PORT_Init (void);
void PCA_Init (void);
void PCA_ISR (void);
void SYSCLK_Init (void);
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
unsigned long b;
WDTCN = 0xde; // disable watchdog timer
WDTCN = 0xad;
SYSCLK_Init ();
PORT_Init (); /*初始化I/O口*/
PCA_Init (); /游扒*PCA初始化成8位脉宽调置方式*/
EIE1= 0x08;
EA=1;
PCA0CPH0=0x20;
while (1)
{ }
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
XBR0 = 0x11; // XBAR0: Initial Reset Value
XBR1 = 0x00; // XBAR1: Initial Reset Value
毕磨巧 XBR2 = 0x40; // XBAR2: Initial Reset Value
// Port configuration (1 = Push Pull Output)
PRT0CF = 0x08; // Output configuration for P0
PRT1CF = 0x00; // Output configuration for P1
PRT2CF = 0x00; // Output configuration for P2
PRT3CF = 0x00; // Output configuration for P3
手键 }
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
void SYSCLK_Init (void)
{
unsigned int i; // delaynms counter
OSCXCN = 0x67; // start external oscillator with
for (i=0; i < 256; i++) ; // XTLVLD blanking interval (>1ms)
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x88; // select external oscillator as SYSCLK
}
//-----------------------------------------------------------------------------
// PCA_Init
//-----------------------------------------------------------------------------
void PCA_Init (void)
{
// PCA0CPM0 = 0x42; // PCA Capture/Compare Register 0
PCA0CN = 0x40;
PCA0CPM1 = 0x42;
PCA0CPL0 = 0x00;
PCA0CPH0 = 0x00;
// PCA0MD &= 0x40;
PCA0MD = 0x01;
}
//-----------------------------------------------------------------------------
// PCA_ISR
//-----------------------------------------------------------------------------
void PCA_ISR (void) interrupt 9
{ unsigned char a ;
bit PWM_PAC;
CF = 0;
if(PWM_PAC==0) //占空比由0 >>0xfe
{
PCA0CPH1=a;
a++;
if(a==0xfe){PWM_PAC=1;}
}
else //占空比由0xfe >>0
{
PCA0CPH1=a;
a--;
if(a==0){PWM_PAC=0;}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
迪凯特科技(北京)有限公司
2023-07-28 广告
2023-07-28 广告
单片机串口通信的原理是利用单片机的串口通信模块,通过把数据转化为一位一位的用时序方式传送数据,实现单片机与外部设备之间的数据通信。
串口通信的概念非常简单,串口按位(bit)发送和接收字节。尽管比按字节(byte)的并行通信慢,但是串口可以...
点击进入详情页
本回答由迪凯特科技(北京)有限公司提供
展开全部
1不用看门狗要使用软件禁止,WDTCN = 0xde;WDTCN = 0xad;不然单片机会一直复位,这个不知道你弄过没有。
2CEX0_Compare_Value = 65536 - 65536;这个你什么意思?想产生0%占空比誉轮的PWM?16位PWM不能产生0%占空比的PWM,想用可以通过禁用PWM实现,绝搏这个芯片资料中提到庆宏信过。把CEX0_Compare_Value改成10000先试试.
2CEX0_Compare_Value = 65536 - 65536;这个你什么意思?想产生0%占空比誉轮的PWM?16位PWM不能产生0%占空比的PWM,想用可以通过禁用PWM实现,绝搏这个芯片资料中提到庆宏信过。把CEX0_Compare_Value改成10000先试试.
追问
1看门狗在另一个模块关了
2这个问题,昨天我也试了,不行
追答
PCA0CN |= 0x40;允许PCA0运行
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
参旦顷考一下这模竖陆个仿纤纳真实例,仿真试试。
追问
我想用C8051F的PCA来实现
追答
程序一样的,端口可以根据你需要的 修改。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询