MCF52259开发板485通信演示程序

2020-02-20 20:05发布

这是MCF52259开发板上写的一个演示485通信的例子。实现的功能很简单,就是通过485收到一个字节就立马再通过485发出去
/*********************************************************
@ Nicrosystem Freescale coldfire MCF52259 Development Board
@ NSCF52259-R1
@ author:bluehacker
@ date:2009-12-01
**********************************************************/
#include "uart.h"


uint8 uart_init(uint8 channel,uint32 sysClockKhz,uint32 baudRate)
{
        uint16 ubgs;
       
        if(channel>2)
        {
                return 1;//error
        }
        set_gpio_uart(channel);
        /*
         * Reset Transmitter
         */
        MCF_UART_UCR(channel) = MCF_UART_UCR_RESET_TX;

        /*
         * Reset Receiver
         */
        MCF_UART_UCR(channel) = MCF_UART_UCR_RESET_RX;

        /*
         * Reset Mode Register
         */
        MCF_UART_UCR(channel) = MCF_UART_UCR_RESET_MR;

        /*
         * No parity, 8-bits per character
         */
        MCF_UART_UMR(channel) = (0
                | MCF_UART_UMR_PM_NONE
                | MCF_UART_UMR_BC_8 );

        /*
         * No echo or loopback, 1 stop bit
         */
        MCF_UART_UMR(channel) = (0
                | MCF_UART_UMR_CM_NORMAL
                | MCF_UART_UMR_SB_STOP_BITS_1);

        /*
         * Set Rx and Tx baud by SYSTEM CLOCK
         */
        MCF_UART_UCSR(channel) = (0
                | MCF_UART_UCSR_RCS_SYS_CLK
                | MCF_UART_UCSR_TCS_SYS_CLK);

        /*
         * Mask all UART interrupts
         */
        MCF_UART_UIMR(channel) = 0;
       
        /*
         * Calculate baud settings
         */
        ubgs = (uint16)((sysClockKhz * 1000)/(baudRate * 32));

        MCF_UART_UBG1(channel) = (uint8)((ubgs & 0xFF00) >> 8);
        MCF_UART_UBG2(channel) = (uint8)(ubgs & 0x00FF);
        /*
         * Enable receiver and transmitter
         */
        MCF_UART_UCR(channel) = (0
                | MCF_UART_UCR_TX_ENABLED
                | MCF_UART_UCR_RX_ENABLED);
       
        return 0;//success
}

void set_gpio_uart(uint8 channel)
{
        switch(channel)
        {
                case 0:
                MCF_GPIO_PUAPAR|=MCF_GPIO_PUAPAR_UTXD0_UTXD0 |
                MCF_GPIO_PUAPAR_URXD0_URXD0 |MCF_GPIO_PUAPAR_URTS0_URTS0 |
                MCF_GPIO_PUAPAR_UCTS0_UCTS0 ;
                break;
               
                case 1:
                MCF_GPIO_PUBPAR|=MCF_GPIO_PUBPAR_UTXD1_UTXD1  |
                MCF_GPIO_PUBPAR_URXD1_URXD1 ;
                break;
               
                case 2:
                MCF_GPIO_PUCPAR|=MCF_GPIO_PUCPAR_UTXD2_UTXD2  |
                MCF_GPIO_PUCPAR_URXD2_URXD2 ;
                break;
               
                default:
                break;
        }
       
}

/*
* Wait for a character to be received on the specified UART
*
* Return Values:
*  the received character
*/
char uart_getchar (uint8 channel)
{
    /* Wait until character has been received */
    while (!(MCF_UART_USR(channel) & MCF_UART_USR_RXRDY))
    {
           
    };

    return (char)MCF_UART_URB(channel);
}


/********************************************************************/
/*
* Wait for space in the UART Tx FIFO and then send a character
*/
void uart_putchar (uint8 channel, char ch)
{
    /* Wait until space is available in the FIFO */
    while (!(MCF_UART_USR(channel) & MCF_UART_USR_TXRDY))
    {
           
    };

    /* Send the character */
    MCF_UART_UTB(channel) = (uint8)ch;
}

/*********************
* send a string using poll mode
*******************/
void uart_putstr(uint8 channel, char *str)
{
        while(*str!=0)
        {
                uart_putchar(channel,*str++);
        }
}
//main.c
/*********************************************************
@ Nicrosystem Freescale coldfire MCF52259 Development Board
@ NSCF52259-R1
@ author:bluehacker
@ date:2009-12-01
**********************************************************/

/*
* main implementation: use this sample to create your own application
*
*/
#include "support_common.h" /* include peripheral declarations and more */
#if (CONSOLE_IO_SUPPORT || ENABLE_UART_SUPPORT)
/* Standard IO is only possible if Console or UART support is enabled. */
#include
#endif
#include "uart.h"
#include "delay.h"

void rs485_init(uint32 baudRate)
{
        MCF_GPIO_PUCPAR &=0x3f;
        MCF_GPIO_DDRUC|=MCF_GPIO_DDRUC_DDRUC3 ;
        MCF_GPIO_PORTUC&=~MCF_GPIO_PORTUC_PORTUC3 ;
        uart_init(1,80000,baudRate);
}

char rs485_getchar()
{
        MCF_GPIO_PORTUC&=~MCF_GPIO_PORTUC_PORTUC3 ;
        return uart_getchar(1);       
}

void rs485_putchar(char ch)
{
        MCF_GPIO_PORTUC|=MCF_GPIO_PORTUC_PORTUC3 ;
        uart_putchar(1,ch);
        while (!(MCF_UART_USR(1) & MCF_UART_USR_TXEMP))
    {
           
    };
        MCF_GPIO_PORTUC&=~MCF_GPIO_PORTUC_PORTUC3 ;
}

int main(void)
{
        int counter = 0;
        char c=0;
       
#if (CONSOLE_IO_SUPPORT || ENABLE_UART_SUPPORT)
        printf("Hello World in C from MCF52259 derivative on MCF52259 board ");
        fflush(stdout);
#endif
        pit_init(0);
        rs485_init(9600);

        for(;;) {          
                   counter++;
           //rs485_putchar(c);
                   c=rs485_getchar();
                   delay_ms(80000,0,10);
                   rs485_putchar(c);
        }
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。