我想检测P00口的边沿触发,不需要计时,不需要计数,只要中断来的时候给个flag,然后我主函数里判断flag的值,相当于产生边沿触发然后像串口发送12345(串口设置肯定没问题),但是就这么简单的程序为什么就是出不来!!!
下面是我写的程序,要是有时间能帮我看一下,感激不尽
#include <C8051F320.h> // SFR declarations
#define BAUDRATE 9600
#define SYSTEMCLOCK 12000000
#include <stdio.h>
#include <string.h>
sbit P00 = P0^0;
unsigned int flag;
unsigned char inbuf[5] = {0};
unsigned char uart_fin = 0;
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
//void UART0_Init (void);
void Port_Init (void); // Port initialization routine
void Timer0_Init (void); // Timer0 initialization routine
void SYSCLK_Init (void);
void PCA0_Init (void);
void PCA0_ISR (void);
void transmit(unsigned char *dat)
{
unsigned int i;
for(i=0;i<5;i++)
{
SBUF0 = dat
;
while(!TI0);
TI0 = 0;
}
}
void PCA0_Init (void)
{
PCA0CPM0 = 0x11;
PCA0CN = 0x00;
EIE1 |= 0x10; // Enable PCA interrupts
}
void PCA0_ISR (void) interrupt 11
{
if (CCF0) // If Module 0 caused the interrupt
{
CCF0 = 0; // Clear module 0 interrupt flag.
flag = 1;
}
}
void main (void)
{
PCA0MD &= ~0x40; // Clear watchdog timer enable
SYSCLK_Init (); // Initialize Oscillator
Timer0_Init (); // Initialize the Timer0
Port_Init (); // Init Ports
PCA0_Init();
UART0_Init();
EA = 1; // Enable global interrupts
while (1)
{
while(flag==1)
{
transmit("12345");
flag = 0;
}
}
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
void Port_Init (void)
{
XBR0 = 0x01; // Enable UART on P0.4(TX) and P0.5(RX)
XBR1 = 0x41; // Enable crossbar
P0MDIN = 0xff;
P0MDOUT = 0x00;
}
void SYSCLK_Init (void)
{
OSCICN |= 0x03; // Configure internal oscillator for
// its maximum frequency
CLKSEL = 0x20;
RSTSRC = 0x04; // Enable missing clock detector
}
void UART0_Init (void)
{
SCON0 = 0x10; // SCON0: 8-bit variable bit rate
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload
TMOD |= 0x20;
TR1 = 1; // START Timer1
TX_Ready = 1; // Flag showing that UART can transmit
IP |= 0x10; // Make UART high priority
ES0 = 1; // Enable UART0 interrupts
}
void Timer0_Init(void)
{
TMOD = 0x01; // Timer0 in 16-bit mode
ET0 = 1; // Timer0 interrupt enabled
TCON = 0x10; // Timer0 ON
}
此帖出自小平头技术问答
要是大家嫌麻烦,可以直接看这一段:核心
void PCA0_Init (void)
{
PCA0CPM0 = 0x11;
PCA0CN = 0x00;
EIE1 |= 0x10; // Enable PCA interrupts
}
void PCA0_ISR (void) interrupt 11
{
if (CCF0) // If Module 0 caused the interrupt
{
CCF0 = 0; // Clear module 0 interrupt flag.
flag = 1;
}
}
另外新的Simplicity Studio也支持C8051了。带有SDK,开发起来比以前的SiliconLabs IDE方便。
插入代码时,使用论坛的“添加代码”功能,就是有两个尖括号的那个图标,这样代码就不会变形了。
一周热门 更多>