📄 pwm_module.c
字号:
#include "Pwm_Module.h"
OS_STK AppTaskPwmStk[256]; // The TaskStack for the PwmTask
#define Address_Of_Voice 0x30000 //changed by michael chen
// The begin address of the voice data in the Flash Rom
vuint8 * PCMaddress = (vuint8 *)Address_Of_Voice; //changed by michael chen
#define VoiceData_Length 0x4000 //the length of VoiceData
INT16U AddAddress =0; //A Global Varible for VoiceData Address Increate;
/****************************************************
Fuction: initialize the Pwm1 Control Register
NOTE: There are at least two Errors in the pwm.h
One is "#define MCF_PWM_PWMDTY1 (*(vuint8 *)(&__IPSBAR[0x1B0019]))"
the right Address is (*(vuint8 *)(&__IPSBAR[0x1B0015]))
Two is "#define MCF_PWM_PWMCNT1 (*(vuint8 *)(&__IPSBAR[0x1B000D]))"
the right Address is (*(vuint8 *)(&__IPSBAR[0x1B001D]))
*****************************************************/
void InitPwmRegister()
{
printf("Begin to Initializing PWM1 Register\n");
//deal with PWM1
MCF_GPIO_DDRTD = MCF_GPIO_DDRTD_DDRTD0; // Set the direction of PTD0 output
MCF_GPIO_PTDPAR |= MCF_GPIO_PTDPAR_PTDPAR0;// Select the IO function as pwm1
MCF_PWM_PWME &= ~MCF_PWM_PWME_PWME1; // Disable the Pwm Chanl 1
MCF_PWM_PWMPOL |= MCF_PWM_PWMPOL_PPOL1; // Begin low level
MCF_PWM_PWMCLK |= MCF_PWM_PWMCLK_PCLK1; // Select SA as Pwm Clock
MCF_PWM_PWMPRCLK &= MCF_PWM_PWMPRCLK_PCKA(0x0); // Clock A prescale is 1
MCF_PWM_PWMCAE &= ~MCF_PWM_PWMCAE_CAE1; //left-Align
MCF_PWM_PWMCTL &= ~MCF_PWM_PWMCTL_CON01; // separate Pwm1 and Pwm2
//comment by michael chen
//MCF_PWM_PWMSCLA = MCF_PWM_PWMSCLA_SCALEA(0x0a); // Now Clock SA Frequency is f_sys/2/10
//add by michael chen
MCF_PWM_PWMSCLA = MCF_PWM_PWMSCLA_SCALEA(0x01); // Now Clock SA Frequency is f_sys/2/10
// MCF_PWM_PWMPER1 = 0x7f;//.MCF_PWM_PWMPER_PERIOD(0x07f);
//comment by michael chen
(*(vuint8 *)(&__IPSBAR[0x1B0015])) = 0x0ff; // Make the Clock_SA frequency is f_SA/255
//(*(vuint8 *)(&__IPSBAR[0x1B0015])) = 0x0fa;//add by michael chen
MCF_PWM_PWMCNT1 = MCF_PWM_PWMCNT_COUNT(0x0ff); // write Any Value to clear the counter
// MCF_PWM_PWMDTY1 = MCF_PWM_PWMDTY_DUTY(0x3F);
(*(vuint8 *)(&__IPSBAR[0x1B001D])) = 0x0af; // write Any Value
// MCF_PWM_PWMCNT1 = MCF_PWM_PWMCNT_COUNT(0x0); // write 0x0ff for example
MCF_PWM_PWME |= MCF_PWM_PWME_PWME1; // Eable the Pwm Chanl 1
printf("End of Initializing PWM1 Register\n");
}
/******************************************************
Function: Set the PIT1 Interrupt Vector
*******************************************************/
void PIT1_Interrupt_Vector_Set(void)
{
printf("Set the Interrupt Vector\n");
OSVectSet(120, (void (*)(void))PIT_1Interrupt); /* Setup the context switch exception */
printf("End of Setting the Interrupt Vector\n");
}
/*********************************************************
Fuction: Initialize PIT1 Interrupt Register
************************************************************/
void InitPIT1Register()
{
printf("Begin to Initializing PIT1 Register\n");
MCF_INTC_ICR56 = 0x09; // Int level 1, priority 0
MCF_PIT1_PCSR &= ~MCF_PIT_PCSR_PIE; //Disable PIT1 Int
MCF_INTC_IMRH &= 0xFEFFFFFF; // Enable PIT1 Int Mask
MCF_PIT1_PMR = 0x2710/2;
MCF_PIT1_PCSR |= MCF_PIT_PCSR_PRE(0x0); // Set the PIT1 Interrupt Period
//Writing PMRn immediately replaces value in PIT counter when counter reaches 0x00
MCF_PIT1_PCSR |= MCF_PIT_PCSR_OVW;
MCF_PIT1_PCSR |= MCF_PIT_PCSR_PIF; // Clear the Interrupt Flag
MCF_PIT1_PCSR |= MCF_PIT_PCSR_RLD; // Counter reloaded from PMRn on count of 0x0000
MCF_PIT1_PCSR |= MCF_PIT_PCSR_PIE; // Enable the PIT1 Interrupt
MCF_PIT1_PCSR |= MCF_PIT_PCSR_EN; // Enable the PIT1 Clock
printf("End of Initializing PIT1 Register\n");
}
/***************************************************
Funciton: The PIT1 Interrupt Function,
Everytime trap in the Interrupt, Read the VoiceData in the flash Rom,
and Write to PWM1 Duty_Register to change the Duty_to_Cycle Ratio of PWM1 Output
NOTE: KeyWord "__interrupt__" Make Compiler to know there is a Interrupt Funtion.
****************************************************/
__interrupt__ void PIT_1Interrupt(void)
{
//added by michael chen
//for debug
//uint16 i = 0;
/**************************************************************************
The following Assemble language is the translation of the C Language;
OSIntNesting ++;
if (OSIntNesting == 1)
{
OSTCBCur = sp;
}
*/
asm(
MOVEQ.L #0,D0
MOVE.B (OSIntNesting),D0
ADDQ.L #1,D0
MOVE.B D0,(OSIntNesting)
CMPI.L #1, d0
BNE _SkipSave
MOVE.L (OSTCBCur), A1
MOVE.L A7,(A1)
);
/***********************************************************************/
_SkipSave:
/**********************************************************************
Read the VoiceData in the flash, and Write to the PWM1 Duty_Register;
if read in the end of the data space, go to the beginning address, and ReRead.
*/
if(AddAddress++ < VoiceData_Length-0x100) // -0x100 in order to discard some unusing Voice data
{
(*(vuint8 *)(&__IPSBAR[0x1B001D])) = *(vuint8*)(PCMaddress+AddAddress+0x100) ;
//for debug michael chen
//printf("%x",*(vuint8*)(PCMaddress+AddAddress+0x100));
//printf("%x\n",(vuint8 *)PCMaddress+AddAddress+0x100);
//for(i = 0;i <10; i++);
}
else
{
AddAddress = 0;
//added by michael chen
(*(vuint8 *)(&__IPSBAR[0x1B001D])) = *(vuint8*)(PCMaddress+AddAddress+0x100) ;
}
/*********************************************************************/
//for debug
//printf("%x\n",*(vuint8 *)(&__IPSBAR[0x1B001D]));
MCF_PIT1_PCSR &= ~MCF_PIT_PCSR_PIE; // Disable Interrupt
MCF_PIT1_PCSR |= MCF_PIT_PCSR_PIF; // Clear the Interrupt Flag
MCF_PIT1_PCSR |= MCF_PIT_PCSR_PIE; // Enable Interrupt
OSIntExit();
}
/*******************************************
The Pwm Task Function,you should Create a Pwm Task in the main() Function Using:
OSTaskCreate(AppTaskPwm, NULL, (void *)&AppTaskPwmStk[255], 5);
Function: Initialize the Pwm Register, PIT1_Control Register, and the PIT1 Interrupt Register;
Set the PIT1 interrupt Register.
********************************************/
void AppTaskPwm (void * pdata)
{
InitPwmRegister(); // Initialize the Pwm Register
PIT1_Interrupt_Vector_Set(); // Set the PIT1 interrupt Register
//for debug michael chen
printf("%x\n",PCMaddress);
printf("%x\n",Address_Of_Voice);
PCMaddress = (vuint8 *)Address_Of_Voice;
printf("%x\n",PCMaddress);
InitPIT1Register(); // Initialize the PIT1 Register
while(1)
{
OSTimeDly(10);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -