PIC單片機讓一個燈閃爍程序
流程圖:
仿真圖:
源代碼:
//-----------------------------------------------------------------
// 名稱: 閃爍的LED
//-----------------------------------------------------------------
// 說明: LED按設(shè)定的時間間隔閃爍
//-----------------------------------------------------------------
#include
#define _XTAL_FREQ 4000000UL
#define LED RC5 //定義LED
//-----------------------------------------------------------------
// 主程序
//-----------------------------------------------------------------
void main()
{
TRISC = 0x00; //設(shè)置整個PORTC端口為輸出(或設(shè)置TRISC5 = 0;)
PORTC = 0xFF; //初始時輸出全1
仿真圖:
源代碼:
//-----------------------------------------------------------------
// 名稱: 閃爍的LED
//-----------------------------------------------------------------
// 說明: LED按設(shè)定的時間間隔閃爍
//-----------------------------------------------------------------
#include
#define _XTAL_FREQ 4000000UL
#define LED RC5 //定義LED
//-----------------------------------------------------------------
// 主程序
//-----------------------------------------------------------------
void main()
{
TRISC = 0x00; //設(shè)置整個PORTC端口為輸出(或設(shè)置TRISC5 = 0;)
PORTC = 0xFF; //初始時輸出全1
while(1)
{
}
}
flowcode軟件用流程圖導(dǎo)出的源代碼:
#define MX_PIC_16BIT
{
}
}
flowcode軟件用流程圖導(dǎo)出的源代碼:
#define MX_PIC_16BIT
//Defines for microcontroller
#define P30F2011
#define MX_CLKS_PER_INST 4
#define MX_SPI1
#define MX_SPI1_PORT_B
#define MX_SPI1_SDI 5
#define MX_SPI1_SDO 4
#define MX_SPI1_SCK 6
#define MX_UART1
#define MX_UART1_PORT_B
#define MX_MI2C1
#define MX_I2C1_PORT_B
#define MX_I2C1_SDA 5
#define MX_I2C1_SCL 4
#define MX_PWM
#define MX_PWM_PORT_D
#define MX_PWM_CNT 2
#define MX_PWM_1 0
#define MX_PWM_2 7
#define P30F2011
#define MX_CLKS_PER_INST 4
#define MX_SPI1
#define MX_SPI1_PORT_B
#define MX_SPI1_SDI 5
#define MX_SPI1_SDO 4
#define MX_SPI1_SCK 6
#define MX_UART1
#define MX_UART1_PORT_B
#define MX_MI2C1
#define MX_I2C1_PORT_B
#define MX_I2C1_SDA 5
#define MX_I2C1_SCL 4
#define MX_PWM
#define MX_PWM_PORT_D
#define MX_PWM_CNT 2
#define MX_PWM_1 0
#define MX_PWM_2 7
//Functions
#define MX_Fosc 48000000
#define MX_Fosc 48000000
#include "../support/dsPIC30F/h/p30fxxxx.h"
#include
#include
#include
#include
#define _RESERVED1(x) int temp1 = (x);
#define _RESERVED2(x) int temp2 = (x);
#define _ICD(x) _FICD(x);
#define _RESERVED2(x) int temp2 = (x);
#define _ICD(x) _FICD(x);
//Configurationdata
//Internal functions
#include "F:Program FilesMatrix MultimediaFlowcode PIC24&dsPIC V4FCDinternals.h"
#include "F:Program FilesMatrix MultimediaFlowcode PIC24&dsPIC V4FCDinternals.h"
//Macro function declarations
//Variable declarations
//Macro implementations
int main()
{
ADPCFG = 0xffff;
while (1)
{
TRISC = TRISC & 0xdfff;
if ((0<<8))
PORTC = (LATC & 0xdfff) | 0x2000;
else
PORTC = LATC & 0xdfff;
delay_ms(255);
delay_ms(245);
{
ADPCFG = 0xffff;
while (1)
{
TRISC = TRISC & 0xdfff;
if ((0<<8))
PORTC = (LATC & 0xdfff) | 0x2000;
else
PORTC = LATC & 0xdfff;
delay_ms(255);
delay_ms(245);
TRISC = TRISC & 0xdfff;
if ((1<<8))
PORTC = (LATC & 0xdfff) | 0x2000;
else
PORTC = LATC & 0xdfff;
if ((1<<8))
PORTC = (LATC & 0xdfff) | 0x2000;
else
PORTC = LATC & 0xdfff;
delay_ms(255);
delay_ms(245);
}
delay_ms(245);
}
mainendloop: goto mainendloop;
return 0;
}
評論