您当前的位置:五五电子网电子知识单片机-工控设备PIC单片机PIC与485的通讯源程序 正文
PIC与485的通讯源程序

PIC与485的通讯源程序

点击数:7304 次   录入时间:03-04 11:59:32   整理:http://www.55dianzi.com   PIC单片机

PIC与485的通讯源程序

#include "HardwareProfile.h"


//Configure bits
    __CONFIG( HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS );
void Board_Init(void);
#if defined(PIC_USE_HC595)
bit b_Reflash;
#endif
#if defined(PIC_USE_KB)
#define Free        1
#define Press        0
bit b_KeyActive;
bit b_KeyPress;
bit b_KeyState;
BYTE u_KeyValue;
BYTE u_SCANACC;
BYTE i_KeyValueBuffer[2];
#endif

BYTE u_10ms_Acc = 0;
BYTE u_50ms_Acc = 0;
BYTE u_100ms_Acc = 0;
BYTE u_200ms_Acc = 0;
WORD i_500ms_Acc = 0;
WORD i_1s_Acc = 0;
BYTE u_frame = 0;
WORD i_ADC;

void interrupt ISR(void)
{
    if(T0IF)
    {
        T0IF = 0;
        TMR0 = 0x0a;            //定时1ms
        u_10ms_Acc ++;
        u_50ms_Acc ++;
        u_100ms_Acc ++;
        u_200ms_Acc ++;
        i_500ms_Acc ++;
        i_1s_Acc ++;
#if defined(PIC_USE_HC595)
        b_Reflash = 1;
#endif
        if(u_10ms_Acc >= 10)
        {
            u_10ms_Acc = 0;
            //在下面增加自己的任务,每10ms执行一次
        }
        if(u_50ms_Acc >= 50)
        {
            u_50ms_Acc = 0;
            //在下面增加自己的任务,每50ms执行一次
#if defined(PIC_USE_KB)
            ADIF = 0;
            ADGO = 1;
#endif
        }
        if(u_100ms_Acc >= 100)
        {
            u_100ms_Acc = 0;
            //在下面增加自己的任务,每100ms执行一次
        }
        if(u_200ms_Acc >= 200)
        {
            u_200ms_Acc = 0;
            //在下面增加自己的任务,每200ms执行一次
        }
        if(i_500ms_Acc >= 500)
        {
            i_500ms_Acc = 0;
            //在下面增加自己的任务,每500ms执行一次
        }
    }
    if(ADIF)
    {
        ADIF = 0;
#if defined(PIC_USE_KB)
        if( ADRESH != 0 )        
        {
            b_KeyPress = 1;    //有按键被按下
            i_ADC = ADRESH;
            i_ADC = i_ADC << 8;
            i_ADC = i_ADC | ADRESL;
            i_ADC = i_ADC & 0xfff8l;
            i_ADC = i_ADC >> 2;
        }
        else
        {
            b_KeyState = Free;    //没按键被按下
            b_KeyPress = 0;
            LED_IO = 0x00;
        }
#endif
    if(TXIF && TXEN)
    {
        TXIF = 0;
    }
    }
}

void main()
{
    Board_Init();
while(1)
{

#if defined(PIC_USE_HC595)
    if(b_Reflash)
    {
        LED_Reflash();
        b_Reflash = 0;
    }
#endif


    if(BUTTON1_IO == 0)
    {
        DisplayNumber_Process(8888);
    }
    if(BUTTON2_IO == 0)
    {
        DisplayNumber_Process(0);
    }


#if defined(PIC_USE_RS485)
    if(RCIF)
    {
        LED0_IO = LED0_IO ^ 1;
        DisplayNumber_Process( RS485_RW() );
    }
#endif

}
}

/////////////////////////////////////////////////////////////////
//Function void Board_Init(void)
//Input:
//        NULL
//Output:
//        NULL
//Overview: 根据实际应用初始化板子上各设备
//        
void Board_Init(void)
{
    BUTTON1_TRIS = 1;
    BUTTON2_TRIS = 1;
    LED_TRIS = 0;

[1] [2]  下一页


本文关键字:源程序  PIC单片机单片机-工控设备 - PIC单片机

《PIC与485的通讯源程序》相关文章>>>