📄 main.c
字号:
#include <stdlib.h>
#include <string.h>
#include "def.h"
#include "option.h"
#include "2410addr.h"
#include "2410lib.h"
#include "2410slib.h"
#include "mmu.h"
#include "2410iic.h"
void Isr_Init(void);
void HaltUndef(void);
void HaltSwi(void);
void HaltPabort(void);
void HaltDabort(void);
void Led_Display(int data);
void PWM_Init(U16 cycle, U16 duty);
void Test_DC_Motor(void);
//如果函数无参数,却又没写void,那么会出现编译warning:Deprecated declaration Test_Step_Motor()-give arg types.
void PWM_Init(U16 cycle, U16 duty)
{
// 参数过滤
if(duty>cycle) duty = cycle;
// 设置定时器2,即PWM周期和占空比
// Fclk=202.8MHz,时钟分频配置为1:2:4,即Pclk=50.7MHz。
rTCFG0 = 255<<8; // 预分频器0设置为99,取得510204Hz
rTCFG1 = rTCFG1&(0xFFFFF0FF)|(3<<8); // TIMER2再取1/2分频,取得255102Hz
rTCMPB2 = duty; // 设置PWM占空比
rTCNTB2 = cycle; // 定时值(PWM周期)
//if(rTCON&0x4000) rTCON = (1<<13); // 更新定时器2数据 (取反输出inverter位)
//else rTCON = (1<<14)|(1<<13);
//rTCON = (1<<12)|(1<<15); // 启动定时器2
rTCON &= 0xffff0fff;
rTCON = (1<<14)|(1<<13);
rTCON &= 0xffff0fff;
rTCON = (1<<15)|(1<<12);
}
//===================================================================
void Main(void)
{
U16 pwm_duty;
S8 flag;
MMU_Init();
ChangeClockDivider(1,1); // 1:2:4
ChangeMPllValue(0xa1,0x3,0x1); // FCLK=202.8MHz,PCLK=50.7MHZ
Port_Init();
Isr_Init();
Uart_Init(0,115200);
Uart_Select(0);
Uart_Printf("\n\n DC Motor TEST\n");
// 设置GPB3为GPIO口
rGPBCON = rGPBCON&(~(0x03<<6))|(0x01<<6); //设置GPB3为GPIO口
rGPBDAT = rGPBDAT&(~(1<<3)); //设置GPB3为0电平
rGPBUP = rGPBUP|(1<<3); //禁止GPB3为上拉电阻
// TOUT2口设置
rGPBCON = rGPBCON&(~(0x03<<4))|(0x02<<4); // rGPBCON[5:4] = 10b,设置TOUT2功能
rGPBUP = rGPBUP|(1<<2); // 禁止TOUT2口的上拉电阻
Uart_Printf("\n S3C2410 test DC_Motor \n");
pwm_duty = 3*255/4; // 初始化占空比为3/4
PWM_Init(255, pwm_duty);
Uart_Printf("\n pwmduty is %d/255! \n", pwm_duty);
while(1)
{
Uart_Printf("\n Please enter 't' to adjust the speed of dc motor (4 level): \n");
flag = Uart_Getch(); // 接收用户通过DNW传入的参数.
if (flag=='t')
{
Uart_Printf("\n the char entered is 't' ! \n");
pwm_duty = pwm_duty + 255/4; // 改变当前电机的速度级别
if(pwm_duty>255)
{
pwm_duty = 3*255/4;
}
Uart_Printf("\n pwmduty is %d/255! \n", pwm_duty);
rTCMPB2 = pwm_duty;
}
}
}
//===================================================================
//Active is low.(LED On)
// GPF7 GPF6 GPF5 GPF4
//nLED_1 nLED2 nLED_4 nLED_3
void Led_Display(int data)
{
// rGPFDAT = (rGPFDAT & 0xf) | !((data & 0xf)<<4);
rGPFDAT = (rGPFDAT & ~(0xf<<4)) | ((~data & 0xf)<<4);
}
//===================================================================
void Isr_Init(void)
{
pISR_UNDEF = (unsigned)HaltUndef;
pISR_SWI = (unsigned)HaltSwi;
pISR_PABORT = (unsigned)HaltPabort;
pISR_DABORT = (unsigned)HaltDabort;
rINTMOD = 0x0; //All=IRQ mode
rINTMSK = BIT_ALLMSK; //All interrupt is masked.
rINTSUBMSK = BIT_SUB_ALLMSK; //All sub-interrupt is masked. <- April 01, 2002 SOP
}
//===================================================================
void HaltUndef(void)
{
Uart_Printf("Undefined instruction exception.\n");
while(1);
}
//===================================================================
void HaltSwi(void)
{
Uart_Printf("SWI exception.\n");
while(1);
}
//===================================================================
void HaltPabort(void)
{
Uart_Printf("Pabort exception.\n");
while(1);
}
//===================================================================
void HaltDabort(void)
{
Uart_Printf("Dabort exception.\n");
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -