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

📄 main.c

📁 32位计数器的计数程序
💻 C
字号:
//*****************************************************************************
//*****************************************************************************
//  FILENAME: main.c
//   Version: 1.0, Updated on 27 July 2004
//
//  DESCRIPTION: Main file of the Example_Counter32_28Pin Project.
//
//-----------------------------------------------------------------------------
//  Copyright (c) Cypress MicroSystems 2000-2003. All Rights Reserved.
//*****************************************************************************
//*****************************************************************************

/*
//----------------------------------------------------------------------------
Project Objective
    To demonstrate the operation of the Counter32 user module of the PSoC microcontroller.  

    NOTE: OPTIONAL C COMPILER IS REQUIRED TO BUILD THIS PROJECT Compiler license is 
    available     for purchase from http://www.onfulfillment.com/cypressstore/ 
    or your local distributor.

Overview
    A 32 bit Counter module is configured as a Pulse Width Modulator (PWM).  The width of 
    the pulse is increased every second and is output to an LED.

    Upon program execution all hardware settings from the device configuration are loaded 
    into the device and main.c is executed. The 24 MHz system clock is directly fed as input 
    to the Counter32 user module.  (Note: Even though the input clock is VC1, this won't be 
    taken as input as the ClockSync parameter is set as Use SysClk Direct, which is 24 MHz).  
    The period of the Counter module is 24000000 (23999999+1), which produces a TerminalCount 
    output after every second.   The Compare value of the counter is initially set to 2400000 
    (ie., 1/10th the period) and increased by 2400000 every second at the TerminalCount ISR, 
    resulting in gradual increase in pulse width every second.  The Compare value is reset 
    once it cross the period value (ie., after 10 seconds).

    The Counter starts counting down from the period value. The CompareOut will be low till 
    such time the Counter value is greater than or equal to Compare value.  Once the Counter 
    value goes below Compare value, the CompareOut goes high.

Project Settings

    Global resources        - No change

    Counter32_Timer
        Clock             = VC1                 Not used, because ClockSync
                                                is set as Use SysClk Direct.
        Enable            = High                Enabled for continuous operation.
        CompareOut        = Row_0_Output_0      Routed thru GlobalOutEven_0
        TerminalCountOut  = None                Not used
        Period            = 23999999            Set to 23999999 and count down to 0.
        CompareValue      = 2400000             1/10th of Period initially.
                                                (increased in code at every    
                                                terminal count).
        CompareType       = Less Than           Once the Counter value is Less
                                                than Compare Value, then drive 
                                                the CompareOut output high.
        InterruptType     = Terminal Count      To generate interrupt after
                                                the count reaches 0.
        ClockSync         = SyncToSysClk        The 24 MHz system clock is
                                                selected as the input clock 
                                                overriding the "Clock" parameter.
        TC_PulseWidth     = Full width          Generate a full pulse width
                                                at every terminal count.
        InvertEnable      = Normal              Not used
    
Input 
    None    
Output
    P0[0]        -    Strong, GlobalOutEven_0
    
How to use this with the Proto board

    CY3210-PSoCEVAL1
        - Connect a wire between P2[0] and LED1 

    CY3210-MiniEval1
        - In JP1, 1 and 2 to be connected  (for 28pin operation)

//----------------------------------------------------------------------------
*/

//-------------------------------------------------------------------
// Include Files
//-------------------------------------------------------------------
#include <m8c.h>        // part specific constants and macros
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules

//-------------------------------------------------------------------
// C Interrupt Handlers
//-------------------------------------------------------------------
#pragma interrupt_handler Counter32_ISR_C()

//-------------------------------------------------------------------
// Variable Allocation
//-------------------------------------------------------------------
DWORD     dNewCompareValue;

//-----------------------------------------------------------------------------
//  FUNCTION NAME: Main
//
//  DESCRIPTION:
//      Main function. Performs system initialization and loops infinitely.
//      
//-----------------------------------------------------------------------------
//
//  ARGUMENTS:        None
//  RETURNS:          Nothing.
//  SIDE EFFECTS:     None.
//
//  THEORY of OPERATION or PROCEDURE:
//    1) Enable Interrupts of user modules and Global Interrupt
//    2) Initialise the compare value (for minimum period)
//    3) Loop infinitely
//
void main()
{
    //Enable Global Interrupt
    M8C_EnableGInt;                            
    
    //Enable the counter
    Counter32_EnableInt();                    
    //Start the counter
    Counter32_Start();                        
     
    //set the initial compare value
    dNewCompareValue = 2400000;                
    
    //Infinite loop. Processing done only at Counter32_ISR_C
    while(1);
}

//-----------------------------------------------------------------------------
//  FUNCTION NAME: Counter32_ISR_C
//
//  DESCRIPTION:
//      Interrupt Service routine of Counter32 usermodule written in C.
//      The _Counter32_ISR subroutine In the Counter32INT.asm file, 
//      redirects the flow to this subroutine.
//-----------------------------------------------------------------------------
//
//  ARGUMENTS:        None
//  RETURNS:          Nothing.
//  SIDE EFFECTS:     None.
//
//  THEORY of OPERATION or PROCEDURE:
//        This ISR is serviced on every Termincal count (ie., every second)
//        The pulse width increased by adjusting the Compare value of the Counter
//
void Counter32_ISR_C()
{    
    //increase the compare value one step so that the pulse width increases
    dNewCompareValue = dNewCompareValue + 2400000;        
    
    //if the the compare value crosses period value, then reset it.
    if ( dNewCompareValue > 24000000 )                    
        dNewCompareValue = 2400000;                        
        
    //Update the new compare value into the Counter       
    Counter32_WriteCompareValue(dNewCompareValue);        
														
}

⌨️ 快捷键说明

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