标题:
波特率自适应技术在DSP5402上的应用之C++测试程序
[打印本页]
作者:
yuyang911220
时间:
2017-2-22 21:43
标题:
波特率自适应技术在DSP5402上的应用之C++测试程序
/*-------------------------------------------------
波特率自适应技术在DSP5402上的应用之C++测试程序
菜农
HotPower@126.com
2005.6.33 21:58 于西安大雁塔村队部
菜地:
http://hotpower.21ic.org
-------------------------------------------------*/
#include <cstdio> //C语言可以用stdio.h
#include <cstring>//C语言可以用string.h
#include "C5402DEF.h"//DSP菜鸟HotPower创建
#include "class.h"
#define ccsdebug 1//1--IO模拟SPI; 0--硬件McBSP模块
extern "C" interrupt void Timer0Isr();//注意加extern "C"
extern "C" interrupt void Timer1Isr();
extern "C" interrupt void Eint0Isr();
extern "C" interrupt void Eint1Isr();
extern "C" interrupt void Eint2Isr();
extern "C" interrupt void Eint3Isr();
SystemObj::SystemObj(void)
{
Disable();//关中断
SREG->IMR = 0x0000;//
SREG->IFR = 0xffff;//清除
//设置0x0080为中断向量表首址,在第2个128页内。
SREG->
MST = (0x01 << PMST_IPTR) | (1 << PMST_MP_MC) | (1 << PMST_OVLY);
// SREG->
MST |= (1 << PMST_DROM);//数据可const.
/*---------------------------------------------------------------------------
若:设置0x0100为中断向量表首址,在第3个128页内。
则:*.cmd文件应该改为:
PAGE 0: VECS: origin=0x0100, length=0x66
则:
SREG->
MST = (0x02 << PMST_IPTR) | (1 << PMST_MP_MC) | (1 << PMST_OVLY);
----------------------------------------------------------------------------*/
SystemInit();//系统初始化
}
void SystemObj::SystemInit(void)
{
PllInit();//PLL初始化
TIMER0Init();
TIMER1Init();
EintInit();
/*--------------------------------------------------------
注意以下2个变量的作用域的不同
--------------------------------------------------------*/
if (::SystemRamTest != 0x55aa) {//全局变量SystemRamTest
::SystemRamTest = 0x55aa;//改写全局变量
}
if (SystemRamTest != 0x55aa) {//类成员变量SystemRamTest
SystemRamTest = 0x55aa;//改写类成员变量
}
::IOXF = 0;
}
void SystemObj:
llInit(void)
{
volatile unsigned int start;
SREG->SWWSR = 0x2000;
do {
SREG->CLKMD = 0;
__nop();
}
while(SREG->CLKMD & (1 << CLKMD_PLLSTATUS));
// SREG->CLKMD = (0 << CLKMD_PLLMUL) | (30 << CLKMD_PLLCOUNT) | (1 << CLKMD_PLLONOFF) | (1 << CLKMD_PLLNDIV) | (1 << CLKMD_PLLSTATUS);//10M
SREG->CLKMD = (9 << CLKMD_PLLMUL) | (30 << CLKMD_PLLCOUNT) | (1 << CLKMD_PLLONOFF) | (1 << CLKMD_PLLNDIV) | (1 << CLKMD_PLLSTATUS);//100M
// SREG->CLKMD = (14 << CLKMD_PLLMUL) | (30 << CLKMD_PLLCOUNT) | (1 << CLKMD_PLLONOFF) | (1 << CLKMD_PLLNDIV) | (1 << CLKMD_PLLSTATUS);//150M
for (start = 0; start < 10000; start ++);//等待外设上电复位正常结束
}
void SystemObj::TIMER0Init(void)
{
TIMER0->TCR = (1 << TCR_TSS);//关闭定时器0
TIMER0->
RD = 60000;//设置定时周期
TIMER0->TIM = 60000;//设置定时计数器
TIMER0->TCR &= ~(1 << TCR_TSS);//启动定时器0
SREG->IMR |= (1 << IMR_TINT0);//允许TINT0中断
}
void SystemObj::TIMER1Init(void)
{
TIMER1->TCR = (1 << TCR_TSS);//关闭定时器1
TIMER1->
RD = 0xffff;//设置定时周期
TIMER1->TIM = 0xffff;//设置定时计数器
// TIMER1->TCR &= ~(1 << TCR_TSS);//启动定时器1
// SREG->IMR |= (1 << IMR_TINT1);//允许TINT1中断
}
void SystemObj::EintInit(void)
{
SREG->IMR |= (1 << IMR_INT0);//允许INT0中断
SREG->IMR |= (1 << IMR_INT1);//允许INT1中断
SREG->IMR |= (1 << IMR_INT2);//允许INT2中断
SREG->IMR |= (1 << IMR_INT3);//允许INT3中断
}
LcdObj:
cdObj(void)
{
LcdInit();
}
void LcdObj:
cdSpiSetup(void)
{
#if (ccsdebug == 0)
/*-----------------------------------------------------------
硬件McBSP模块配置为SPI接口
实验目的:
演练硬件McBSP模块配置为SPI接口的应用。
------------------------------------------------------------*/
McBSP1->SPSA = SPCR1;
McBSP1->SPSD = 0;
// McBSP1->SPSD &= ~(1 << SPCR1_RRST);//禁止串口接收
_delay_loop_(1);//延时等待
McBSP1->SPSA = SPCR2;
McBSP1->SPSD = 0;
// McBSP1->SPSD &= ~(1 << SPCR2_XRST);//禁止串口发送
_delay_loop_(1);//延时等待
McBSP1->SPSA = SPCR1;
McBSP1->SPSD = (0x02 << SPCR1_CLKSTP);//CLKSTP=10b
_delay_loop_(1);//延时等待
McBSP1->SPSA = PCR;
McBSP1->SPSD = (1 << PCR_FSXM) //设置FSX引脚为输出,控制LCD12864的片选信号SS
| (0 << PCR_FSXP) //FSX引脚平时输出低电平(LCD12864的片选信号SS无效)
| (1 << PCR_CLKXM) //设置CLKX引脚为输出
| (1 << PCR_CLKXP);//CLKXP引脚平时输出高电平
_delay_loop_(1);//延时等待
McBSP1->SPSA = SRGR1;
McBSP1->SPSD = (0x07 << SRGR1_FWID) //0x00~0x07~0xff
| (0x80 << SRGR1_CLKGDV);//速率0x80
_delay_loop_(1);//延时等待
McBSP1->SPSA = SRGR2;
McBSP1->SPSD = (1 << SRGR2_CLKSM);//
_delay_loop_(1);//延时等待
McBSP1->SPSA = RCR1;
McBSP1->SPSD = (0x00 << RCR1_RFRLEN1);//8BIT
_delay_loop_(1);//延时等待
McBSP1->SPSA = XCR1;//延时等待
McBSP1->SPSD = (0x00 << XCR1_RFRLEN1);//8BIT
_delay_loop_(1);//延时等待
McBSP1->SPSA = RCR2;
McBSP1->SPSD = (0x02 << RCR2_RDATDLY);//0x01
_delay_loop_(1);//延时等待
McBSP1->SPSA = XCR2;
McBSP1->SPSD = (0x02 << XCR2_RDATDLY);//0x01
_delay_loop_(1);//延时等待
McBSP1->SPSA = SPCR2;
McBSP1->SPSD |= (1 << SPCR2_GRST) | (1 << SPCR2_XRST) | (1 << SPCR2_FRST);
_delay_loop_(1);//延时等待
#else
/*-----------------------------------------------------------
硬件McBSP模块配置为普通IO软件模拟SPI时序
实验目的:
演练硬件McBSP模块配置为普通IO的应用.以备IO紧缺时急用。
------------------------------------------------------------*/
McBSP1->SPSA = SPCR1;
McBSP1->SPSD = 0;
McBSP1->SPSD &= ~(1 << SPCR1_RRST);//禁止串口接收
McBSP1->SPSA = SPCR2;
McBSP1->SPSD = 0;
McBSP1->SPSD &= ~(1 << SPCR2_XRST);//禁止串口发送
McBSP1->SPSA = PCR;
McBSP1->SPSD = (1 << PCR_XIOEN) | (1 << PCR_RIOEN);//设置收发为IO接口,DX输出,DR,CLKS输入
McBSP1->SPSD |= (1 << PCR_FSXM) | (1 << PCR_CLKXM);//设置FSX,CLKX可做IO输出
McBSP1->SPSD |= (1 << PCR_FSRM) | (1 << PCR_CLKRM);//设置FSR,CLKR可做IO输出
McBSP1->SPSD &= ~(1 << PCR_FSXP);//FSX信号=0
McBSP1->SPSD |= (1 << PCR_CLKXP);//CLKX信号=1
McBSP1->SPSD |= (1 << PCR_DX_STAT);//DX信号=1
#endif
}
void LcdObj:
cdInit(void)
{
_delay_loop_(1000);//延时等待
LcdSpiSetup();
/*---------------------------------------------------
LCD模块上电等待延时
----------------------------------------------------*/
_delay_loop_(1000);//延时等待
LcdClearBuffer();
LcdSendCommand(0x20);//发送4位控制命令
// LcdSendCommand(0x30);//发送8位控制命令//与8位4位无关!!!
LcdSendCommand(0x02);//发送位址归位命令,设定DDRAM位址计数器为0
LcdSendCommand(0x04);//发送进入点命令
LcdSendCommand(0x0c);//发送开显示关光标命令
LcdSendCommand(0x01);//发送清除显示命令
LcdSendCommand(0x80);//发送设定DDRAM地址0x00命令
//Keil C51汉字0xfd出错测试
// SetLcdDisplayPos(0, 0);//汉字定位到上行左端
// LcdDisplay("褒饼昌除待谍洱俘");
// LcdDisplay("1234567812345678");
// SetLcdDisplayPos(1, 0);//汉字定位到上行左端
// LcdDisplay("庚过糊积箭烬君魁");
// LcdDisplay("1234567812345678");
// SetLcdDisplayPos(2, 0);//汉字定位到上行左端
// LcdDisplay("例笼慢谬凝琵讫驱");
// LcdDisplay("1234567812345678");
// SetLcdDisplayPos(3, 0);//汉字定位到上行左端
// LcdDisplay("三升数她听妄锡淆");
// LcdDisplay("1234567812345678");
}
void LcdObj:
cdSend(unsigned char cData)
{
cData &= 0xff;
#if (ccsdebug == 0)
McBSP1->DXR1 = cData;//发送串行数据或命令
_delay_loop_(1);//延时等待
McBSP1->SPSA = SPCR2;
while((McBSP1->SPSD & (1 << SPCR2_XRDY)) == 0) {//等待发送结束
__nop();
__nop();
}
_delay_loop_(1);//延时等待
#else
unsigned int i;
McBSP1->SPSA = PCR;
for (i = 0; i < 8; i ++) {
McBSP1->SPSD &= ~(1 << PCR_CLKXP);//CLKX信号=0
_delay_loop_(1);//延时等待
if (cData & 0x80) {//MSB最高位为1时
McBSP1->SPSD |= (1 << PCR_DX_STAT);//DX信号=1
}
else {
McBSP1->SPSD &= ~(1 << PCR_DX_STAT);//DX信号=0
}
cData <<= 1;
_delay_loop_(1);//延时等待
McBSP1->SPSD |= (1 << PCR_CLKXP);//CLKX信号=1
_delay_loop_(1);//延时等待
}
McBSP1->SPSD |= (1 << PCR_DX_STAT);//DX信号=1
_delay_loop_(1);//延时等待
#endif
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0