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

📄 main.c

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

/*----------------------------------------------------------------------------
Project Objective
    To demonstrate the operation of the Counter24 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 24 bit Counter module counts the number of switch closures in 5 seconds and displays 
    the same on the LCD.   In this project, a counter24 is configured as a timer to get the 
    required Interrupt after every 5 seconds.

    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 divided by 16 (VC1) 
    to produce a 1.5 MHz clock. This is given as input clock to the Counter24 user module 
    named "Counter24_Timer" which has a period of 7500000 (7499999+1), effectively producing 
    a TerminalCount interrupt at a duration of 5 seconds.  Another Counter24 user module 
    named "Counter24_Counter" has the input clock from P1[4] connected to a switch, which 
    goes high when closed.   As and when the switch is closed, this "Counter" registers it. 

    At every 5 seconds the Counter24_Timer generates an interrupt, during which the total 
    counts registered in the Counter24_Counter is output on the LCD and then it is reset 
    to start a fresh count for next five seconds.
    
    (Note : The switch has to be pressed properly to get the correct count)

Project Settings
    Global resources            
        VC1               = 16                 divide 24MHz clock by 16

    Counter24_Timer
        Clock             = VC1                Input is 1.5 MHz clock
        Enable            = High               Enabled for continuous operation.
        CompareOut        = None               Not used.
        TerminalCountOut  = None               Not used.
        Period            = 7499999            Set to 7499999 and count down to 0.
        CompareValue      = 0                  Not used.
        CompareType       = Less Than          Not used.
        InterruptType     = Terminal Count     To generate interrupt after
                                               the count reaches 0.
        ClockSync         = SyncToSysClk       Synchronize to system clock.
        TC_PulseWidth     = Full width         Generate a full pulse width
                                               at every terminal count.
        InvertEnable      = Normal             Not used.
    

    Counter24_Counter
        Clock             = Row_0_Input_0      Routed thru GlobalInOdd_4
        Enable            = High               Enabled for continuous operation.
        CompareOut        = None               Not used.
        TerminalCountOut  = None               Not used
        Period            = 99                 Set to 99 and count down to 0.
        CompareValue      = 0                  Not used.
        CompareType       = Less Than          Not used.
        InterruptType     = Terminal Count     Not used.
        ClockSync         = SyncToSysClk       Synchronize to system clock.
        TC_PulseWidth     = Full width         Not used.
        InvertEnable      = Normal             Not used.

    LCD
        LCDPort           = Port2              LCD at Port2.
        BarGraph          = Disable            Disable the BarGraph option of LCD.

Input 
    
    P1[4]          -    High Z, GlobalInOdd_4    Input from Switch.
    
Output
    P2[0]-P2[6]    -    Strong                    LCD output.        

How to use this with the Proto board

    CY3210-PSoCEVAL1
    - Connect an industry standard Hitachi HD44780 based LCD module in the LCD header.

    - Connect a 4.7k resistor from GND Pin of J5 to breadboard and from that point connect 
      a wire to P1[4] and from the same point connect a wire to SW (switch) in J5.  ie., in 
      simple terms, the P1[4] is pulled down to ground through a resistor and connected to 
      a switch, which connects to Vcc when closed.

CY3210-MiniEval1
    - In JP1, 1 and 2 to be connected  (for 28 pin operation)
    - Connect an industry standard Hitachi HD44780 based LCD module to Port2 as per 
      the diagram shown in datasheet of PSoC LCD user module.

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

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

//-------------------------------------------------------------------
// C Interrupt Handlers
//-------------------------------------------------------------------
#pragma interrupt_handler Counter24_Timer_ISR_C        //for Counter24_Timer

//-----------------------------------------------------------------------------
//  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) Loop infinitely
//
void main()
{
    // Initialize and start the LCD
    LCD_Start();                            

    // enable the global interrupt
    M8C_EnableGInt;                            
    
    // enable the Counter interrupt mask
    Counter24_Counter_EnableInt();        
    // start the Counter    
    Counter24_Counter_Start();                
    
    // enable the Counter(Timer) interrupt mask
    Counter24_Timer_EnableInt();            
    // start the Counter(Timer)
    Counter24_Timer_Start();                

    //Position the LCD to Row 1 column 1        
    LCD_Position(0,0);                        
    LCD_PrCString("Keep Pressing");            
    
    //Position the LCD to Row 2 column 1        
    LCD_Position(1,0);                        
    LCD_PrCString("Switch S1");                
    
    // Infinate loop.  Processing done only at the Counter24_Timer_ISR_C
    while(1);                                
}

//-----------------------------------------------------------------------------
//  FUNCTION NAME: Counter24_Timer_ISR_C
//
//  DESCRIPTION:
//      Interrupt Service routine of Counter24_Timer usermodule written in C.
//      The Counter24_Timer_ISR subroutine In the Counter24_TimerInt.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 5 second)
//        Read the Counter24_Counter value and find out the elapsed count
//        and output on to the LCD.
//
void Counter24_Timer_ISR_C()
{
    //Initialize variables
    DWORD    dElapsedCount;
    char     sOutputStr[3];
    
    dElapsedCount = 0;
    
    //Read the counter and store the value in dElapsedCount
    Counter24_Counter_ReadCounter(&dElapsedCount);    
    
    //Clear the LCD
    LCD_Control(0x01);                                
    
    //subtract the counter Value from 99 (ie., the period) to get the elapsed count.
    dElapsedCount = 99 - dElapsedCount;                

    //convert the value of elapsed count to ascii using Interger to Ascii function (itoa)
    itoa(sOutputStr,dElapsedCount,10);                

    //Position the LCD to Row 1 column 1        
    LCD_Position(0,0);                                
    LCD_PrCString("Switch S1 was");                    
    
    //Position the LCD to Row 2 column 1        
    LCD_Position(1,0);                            
    LCD_PrCString("pressed ");                        
    
    //Send the elapsed count to LCD
    LCD_PrString(sOutputStr);                        
    LCD_PrCString(" times");                        
    
    //stop the counter before reloading the period. (if counter is stopped, then 
    //the period value when written, latches onto counter value)
    Counter24_Counter_Stop();                        

    //reload the write period so that the counter starts a fresh count down.
    Counter24_Counter_WritePeriod(99);                
                                                    
    //restart the counter
    Counter24_Counter_Start();                        
}

⌨️ 快捷键说明

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