⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 IAR编译环境下的AT91SAM7S64芯片的PWM输出演示
💻 C
字号:
//*----------------------------------------------------------------------------
//*         ATMEL Microcontroller Software Support  -  ROUSSET  -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name              : main.c
//* Object                 : main application written in C
//* Creation               : S.CADENE   01/September/2004
//* Vers                   : V1.0
//* 1.1  05/April/2005 JPP : Addicting some feature
//*----------------------------------------------------------------------------

// Include Standard files
#include "Board.h"
#include "init.h"
#include "lib_PWM.h"
#include "Audio.h"
#include "wave.h"
#include "..\mips\mips.h"

/*  In this project we are assuming to have master clock equal to  47.9232 MHz
    on the AT91SAM7SEK.*/

static const char POLLING_header[]={"\n\rATMEL PWM\n\r"};
char message[80];


//*----------------------------------------------------------------------------
//* Function Name       : menu
//* Object              : Usart debug menu
//*----------------------------------------------------------------------------
void Menu( char value)
{
 unsigned int size;
	switch (value) {
	case '0': //* info
	     sprintf((char*) message,"Version   0x%X\n\r",(unsigned int) *AT91C_PWMC_VR );
             AT91F_DBGU_Printk((char*)message);
             sprintf((char*) message,"AT91C_PWMC_SR  0x%X (1)\n\r",(unsigned int) *AT91C_PWMC_SR);
             AT91F_DBGU_Printk((char*)message);
             sprintf((char*) message,"AT91C_PWMC_IMR 0x%X (1)\n\r",(unsigned int) *AT91C_PWMC_IMR);
             AT91F_DBGU_Printk((char*)message);
             sprintf((char*) message,"PWMC_MR        0x%X\n\r",(unsigned int) *AT91C_PWMC_MR );
             AT91F_DBGU_Printk((char*)message);

             AT91F_DBGU_Printk("Channel 0\n\r");
             sprintf((char*) message,"PWMC_CMR   0x%X\n\r",(unsigned int) AT91C_BASE_PWMC_CH0->PWMC_CMR );
             AT91F_DBGU_Printk((char*)message);
             sprintf((char*) message,"PWMC_CCNTR 0x%X (Counter Register)\n\r",(unsigned int) AT91C_BASE_PWMC_CH0->PWMC_CCNTR );
             AT91F_DBGU_Printk((char*)message);
             sprintf((char*) message,"PWMC_CDTYR 0x%X (Duty cycle)\n\r",(unsigned int) AT91C_BASE_PWMC_CH0->PWMC_CDTYR);
             AT91F_DBGU_Printk((char*)message);
             sprintf((char*) message,"PWMC_CPRDR 0x%X (bit size)\n\r",(unsigned int) AT91C_BASE_PWMC_CH0->PWMC_CPRDR);
             AT91F_DBGU_Printk((char*)message);

             sprintf(message,"\n\rMCK: %d Hz\n\r",MCK );
             AT91F_DBGU_Printk(message);
     	     sprintf(message,"MC_FMR: 0x%X\n\r", AT91C_BASE_MC->MC_FMR);
             AT91F_DBGU_Printk(message);
	break;
	case '1': //* MIPS
	    //* Enable the clock and Start timer
	        Start_Mips_Timer();
	   //* Start the target application Toggle the LED2.
            Trace_Toggel_LED(LED2);
      //* Interrupt measurement
	        THUMB_mips();
            Trace_Toggel_LED(LED2);
	  //* print result
	      sprintf(message,"Rest %d, %d mMIPS\n\r", Mips_Result_loop(), Mips_Result_all() );
	      AT91F_DBGU_Printk(message);
	break;
	case '2': //* Start
          AT91F_PWMC_StartChannel(AT91C_BASE_PWMC,AT91C_PWMC_CHID0);
	      AT91F_DBGU_Printk("Start Channel 0 \n\r");
	break;
	case '3': //* stop
	      AT91F_PWMC_StopChannel(AT91C_BASE_PWMC,AT91C_PWMC_CHID0);
	      AT91F_DBGU_Printk("Stop Channel 0 \n\r");
	break;
	case '4': //* Constant
           //* set constant
  	       AT91F_DBGU_Printk("Constant (0-255):");
           AT91F_DBGU_scanf("%d",&size);
    	   SetConstant((char) size);
           sprintf(message,"\n\rValue: %d \n\r", (char) size);
           AT91F_DBGU_Printk(message);
           OutputSample_init(OutputConstant);
	break;
	case '5': //* Ramp
  	       AT91F_DBGU_Printk("Ramp freq (0-4000):");
           AT91F_DBGU_scanf("%d",&size);
    	   SetRamp(size );
           sprintf(message,"\n\rValue: %d \n\r", size);
           AT91F_DBGU_Printk(message);
           OutputSample_init(OutputRamp);
	break;
	case '6': //* Sinus
	       SinusInit();
  	       AT91F_DBGU_Printk("Sinus freq (0-4000):");
           AT91F_DBGU_scanf("%d",&size);
    	   SetSinus(size );
           sprintf(message,"\n\rValue: %d \n\r", size);
           AT91F_DBGU_Printk(message);
           OutputSample_init(OutputSinus);
	break;
	case '7': //* Wave
  	       AT91F_DBGU_Printk("Wave:");
           AT91F_DBGU_scanf("%d",&size);
    	   Setwave();
           sprintf(message,"\n\rValue: %d \n\r", size);
           AT91F_DBGU_Printk(message);
           OutputSample_init(OutputWave);
	break;
	default:
		AT91F_DBGU_Printk("\n\r");
	  break;
	}
}//* End
//*----------------------------------------------------------------------------
//* Function Name       : key
//* Object              : Usart debug menu
//*----------------------------------------------------------------------------
void KeyMenu( void )
{
	if ( (AT91F_PIO_GetInput(AT91C_BASE_PIOA) & SW1_MASK ) != SW1_MASK )
	{
	   Trace_Toggel_LED(LED3);
       AT91F_PWMC_StartChannel(AT91C_BASE_PWMC,AT91C_PWMC_CHID0);
       SetConstant(80);
       OutputSample_init(OutputConstant);
    }
	if ( (AT91F_PIO_GetInput(AT91C_BASE_PIOA) & SW2_MASK ) != SW2_MASK )
	{
	   Trace_Toggel_LED(LED3);
       AT91F_PWMC_StartChannel(AT91C_BASE_PWMC,AT91C_PWMC_CHID0);
	    SinusInit();
        SetSinus(800);
        OutputSample_init(OutputSinus);
    }
	if ( (AT91F_PIO_GetInput(AT91C_BASE_PIOA) & SW3_MASK ) != SW3_MASK )
	{
	   Trace_Toggel_LED(LED3);
       AT91F_PWMC_StartChannel(AT91C_BASE_PWMC,AT91C_PWMC_CHID0);
       Setwave();
       OutputSample_init(OutputWave);
    }
	if ( (AT91F_PIO_GetInput(AT91C_BASE_PIOA) & SW4_MASK ) != SW4_MASK )
	{
	   Trace_Toggel_LED(LED3);
       AT91F_PWMC_StopChannel(AT91C_BASE_PWMC,AT91C_PWMC_CHID0);
    }
}
//*--------------------------------------------------------------------------------------
//* Function Name       : Main
//* Object              : Software entry point
//*--------------------------------------------------------------------------------------
int main()
{
   char value;
    // Enable User Reset and set its minimal assertion to 960 us
       AT91C_BASE_RSTC->RSTC_RMR = AT91C_RSTC_URSTEN | (0x4<<8) | (unsigned int)(0xA5<<24);

    // First, enable the clock of the PIO
       AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1 << AT91C_ID_PIOA ) ;
    // to be outputs. No need to set these pins to be driven by the PIO because it is GPIO pins only.
       AT91F_PIO_CfgOutput( AT91C_BASE_PIOA, LED_MASK ) ;
    // Clear the LED's. On the board we must apply a "1" to turn off LEDs
       AT91F_PIO_SetOutput( AT91C_BASE_PIOA, LED_MASK ) ;
    //* open external PIO interrupt
        //* define switch SW3 at PIO input for interrupt IRQ loop
    	AT91F_PIO_CfgInput(AT91C_BASE_PIOA, SW_MASK );

    //* To display the MCK clock by using PCK2 on PA31 IO line
        AT91F_PIO_CfgPeriph(AT91C_BASE_PIOA, 0, AT91C_PA31_PCK2);
        AT91C_BASE_PMC->PMC_SCER = 0x401;
        AT91C_BASE_PMC->PMC_PCKR[2]=0x3;

    // Set the LED1.
       Trace_Toggel_LED(LED1);
    //* Init DBGU
        AT91F_DBGU_Init();
    //* Polling
        AT91F_DBGU_Printk((char*)POLLING_header);
    //* Timer initialization
	    Init_Mips_Timer();

        AT91F_PWM_Open();

        AT91F_DBGU_Printk("0)Info\n\r1)MIPS\n\r2)Open PWB\n\r3)Close PWB\n\r4)Const\n\r5)Ramp\n\r6)Sinus\n\r7)Wave\n\r");

    //* Infinit loop
    while(1)
    {
	if (AT91F_DBGU_Get(&value) == 0 ) {
	   Menu(value);
	} else {
       KeyMenu();
	}

    }// End while

}
//* End

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -