单片机 > 单片机程序设计 > 详情

485通讯PIC源程序

发布时间:2020-06-19 发布时间:
|

#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 <            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; 
    LED_IO = 0x01; 
    TMR0_Init(); 
    #if defined(PIC_USE_HC595) 
        HC595_Init(); 
    #endif 

    #if defined(PIC_USE_LCD12864) 
        LCD_Init(); 
    #endif 

    #if defined(PIC_USE_ISD1700) 
        ISD1700_Init(); 
    #endif 

    #if defined(PIC_USE_KB) 
        KeyBoard_Init(); 
    #endif 

    #if defined(PIC_USE_RS485) 
        RS485_Init(); 
    #endif 
}


#include
#include "HardwareProfile.h"
#if defined(PIC_USE_RS485)


void RS485_Init(void)
{
 RS485_DIR_IO = Receive;
 RS485_DIR_TRIS = 0;
 RS485_RX_TRIS = 1;
 RS485_TX_TRIS = 0;
/* SPBRG = DIVIDER;
 RCSTA = (NINE_BITS|0x90);
 TXSTA = (SPEED|NINE_BITS|0x20);*/
 //SPBRG=25;        //波特率为 9600       Baud Rate = Fosc/(16*(SPBRG+1))
 SPBRG=64;
 BRGH=1;          //高速波特率使能位
 TXEN=1;          //发送允许
 CREN=1;          //连续接收选择位
 SPEN=1;          //串行口使能位
 TXIE=0;  
 RCIE=0;          //接收中断使能开启
 TXIF=0;
 RCIF=0;
 PEIE=1;
 GIE=1;
}

void RS485_Putch(BYTE byte)
{

 /* output one byte */
 while(!TXIF) /* set when register is empty */
  continue;
 RS485_DIR_IO = Send;
 TXREG = byte;
 while(!TRMT) /* set when register is empty */
  ;
 RS485_DIR_IO = Receive;
}

BYTE RS485_Getch(void)
{
 /* retrieve one byte */
 while(!RCIF) /* set when register is not empty */
  continue;
 RCIF = 0;
 return RCREG;
}

BYTE RS485_RW(void)
{
 BYTE temp;
 RS485_Putch(temp = RS485_Getch());
 return temp;
}

#endif


关键字:485通讯  PIC  源程序 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
PIC单片机基础知识之二