//--------------------------------------------------------------------------------
#include <stdio.h> /* prototype declara
tions for I/O functions */
#include <LPC21xx.H> /* LPC21xx definitions */
#include "Main.h"
//--------------------------------------------------------------------------------
void UART1_Init (void)
{
UINT16 Fdiv;
PINSEL0 = 0x00050000;
U1LCR = 0x83; // DLAB = 1,可设置波特率
Fdiv = (Fpclk / 16)/ UART_BPS; // 设置波特率
U1DLM = Fdiv / 256;
U1DLL = Fdiv % 256;
U1LCR = 0x03;
}
/*****************************PLL初始化**************************************/
void PLL_Init(void)
{
/* 设置系统各部分时钟 */
PLLCON = 1;
#if ((Fcclk / 4) / Fpclk) == 1
VPBDIV = 0;
#endif
#if ((Fcclk / 4) / Fpclk) == 2
VPBDIV = 2;
#endif
#if ((Fcclk / 4) / Fpclk) == 4
VPBDIV = 1;
#endif
#if (Fcco / Fcclk) == 2
PLLCFG = ((Fcclk / Fosc) - 1) | (0 << 5);
#endif
#if (Fcco / Fcclk) == 4
PLLCFG = ((Fcclk / Fosc) - 1) | (1 << 5);
#endif
#if (Fcco / Fcclk) == 8
PLLCFG = ((Fcclk / Fosc) - 1) | (2 << 5);
#endif
#if (Fcco / Fcclk) == 16
PLLCFG = ((Fcclk / Fosc) - 1) | (3 << 5);
#endif
PLLFEED = 0xaa;
PLLFEED = 0x55;
while((PLLSTAT & (1 << 10)) == 0);
PLLCON = 3;
PLLFEED = 0xaa;
PLLFEED = 0x55;
}
//--------------------------------------------------------------------------------
int main (void)
{
//MAM_init(); //硬件加速
//PLL_Init(); //
芯片内部锁相环设置(系统和各部份工作频率设置)
//UART1_Init(); //串口初始化和设置波特率
Timer0_Init();
GPIO_init(LED1); //设置端口为GPIO
GPIO_init(LED2); //设置端口为GPIO
GPIO_init(LED3); //设置端口为GPIO
GPIO_init(LED4); //设置端口为GPIO
GPIO_init(BUZZER);
// GPIO_init(LED5); //设置端口为GPIO
// GPIO_init(LED6); //设置端口为GPIO
GPIO_inout(LED1,OUTPUT); //设置端口为输出模式
GPIO_inout(LED2,OUTPUT); //设置端口为输出模式
GPIO_inout(LED3,OUTPUT); //设置端口为输出模式
GPIO_inout(LED4,OUTPUT); //设置端口为输出模式
GPIO_inout(BUZZER,OUTPUT);
// GPIO_inout(LED5,OUTPUT); //设置端口为输出模式
// GPIO_inout(LED6,OUTPUT); //设置端口为输出模式
GPIO_SET(LED1,HIGH);
GPIO_SET(LED2,HIGH);
GPIO_SET(LED3,HIGH);
GPIO_SET(LED4,HIGH);
GPIO_SET(BUZZER,HIGH);
// GPIO_SET(LED5,HIGH);
// GPIO_SET(LED6,HIGH);
while(TRUE)
{
}
}
void GPIO_init(UINT8 GPIO_num)
{
if(GPIO_num < 16)
PINSEL0 &= ~(0x03 << (GPIO_num * 2));
else
PINSEL1 &= ~(0x03 << ((GPIO_num-16) * 2));
}
void GPIO_inout(UINT8 GPIO_num,UINT8 in_out)
{
if(in_out)
IODIR0 |= (0x01 << GPIO_num);//output
else
IODIR0 &= ~(0x01 << GPIO_num); //input
}
UINT8 GPIO_READ(UINT8 GPIO_num)
{
if(IOPIN0 & (1 << GPIO_num))
return 1;
else
return 0;
}
void MAM_init(void)
{
MAMCR = 0x02;
MAMTIM = 0x03;
}
/****************************************************************************
* 名称:Time0Init()
* 功能:初始化定时器0,定时时间为1S。
* 入口参数:无
* 出口参数:无
****************************************************************************/
void Timer0_Init(void)
{
/* Fcclk = Fosc*4 = 11.0592MHz*4 = 44.2368MHz
Fpclk = Fcclk/4 = 44.2368MHz/4 = 11.0592MHz //016.5888
*/
T0PR = 99; // 设置定时器0分频为100分频,得110592Hz
T0MCR = 0x03; // 匹配通道0匹配中断并复位T0TC
T0MR0 = 165888/2; // 比较值(1S定时值)
T0TCR = 0x03; // 启动并复位T0TC
T0TCR = 0x01;
//while( (T0IR & 0x01) == 0 ); // 等待定时时间到
// T0IR = 0x01; // 清除中断标志
/* 设置定时器0中断IRQ */
VICIntSelect = 0x00; // 所有中断通道设置为IRQ中断
VICVectCntl0 = 0x20 | 4; // 设置定时器0中断通道分配最高优先级
VICVectAddr0 = (UINT32)IRQ_Timer0; // 设置中断服务程序地址
VICIntEnable = (1 << 0x04); // 使能定时器0中断
}
void __irq IRQ_Timer0 (void)
{
static UINT8 i = 0;
GPIO_SET(LED1,HIGH);
GPIO_SET(LED2,HIGH);
GPIO_SET(LED3,HIGH);
GPIO_SET(LED4,HIGH);
GPIO_SET(BUZZER,HIGH);
// GPIO_SET(LED5,HIGH);
//GPIO_SET(LED6,HIGH);
switch(i)
{
case 0:
i++;
GPIO_SET(LED1,LOW);
break;
case 1:
i++;
GPIO_SET(LED2,LOW);
break;
case 2:
i++;
GPIO_SET(LED3,LOW);
break;
case 3:
i=0;
GPIO_SET(LED4,LOW);
GPIO_SET(BUZZER,LOW);
break;
}
T0IR = 0x01; /* 清除中断标志 */
VICVectAddr = 0x00; /* 通知VIC中断处理结束 */
}
一周热门 更多>