main.c
/********************************************************************************/
/* */
/* Copyright (c) 2016, Lao Ma Studio */
/* All rights reserved. */
/* */
/* Email:pcwebmaster@163.com */
/********************************************************************************/
/************************************************************************************/
/* File name: comint.c */
/* Version: Version 1.0 */
/* Description: Serial port interrupt with communication protocol test */
/* Main control chip: MSP430G2553 */
/* Crystal oscillator frequency: internal 1MHz */
/* Compilation environment: CCS5.4 Grace initialization */
/* Author: pcwebmaster (Arctic Wolf) */
/* Function: */
/* system_init */
/* com_send_command */
/* com_command_receive */
/* CalCRC16_1021 */
/* command_decoder */
/* send_command */
/* Test: Send: 16 16 02 01 02 01 00 35 03 94 BD Receive: 49 AA 15 */
/* Test phenomenon: */
/* Historical record: 20016.02.23 test passed */
/* Arctic Wolf 20016-02-23 Creat Inital version. (Version 1.0) */
/****************************************************************************/
#include <msp430.h>
/* ======== Grace related includes ======== */
#include <ti/mcu/msp430/Grace.h>
unsigned char *str = "Hello World!\n\r Serial test normal!\n\r";
unsigned char *str1= "Serial port test normal!\n\r";
/* ======== main ======== */
int main(void)
{
Grace_init(); // Activate Grace-generated configuration
buff_init();
// >>>>> Fill -in user code here <<<<<
Send_String(str);//Test
Send_String(str1);//Test
while(1)
{
com_command_receive();
}
return (0);
}
Comint.c
/********************************************************************************/
/* */
/* Copyright (c) 2016, Lao Ma Studio */
/* All rights reserved. */
/* */
/* Email:pcwebmaster@163.com */
/****************************************************************************/
/************************************************************************************/
/* File name: comint.c */
/* Version: Version 1.0 */
/* Description: Serial port interrupt with communication protocol test */
/* Main control chip: MSP430G2553 */
/* Crystal oscillator frequency: internal 1MHz */
/* Compilation environment: CCS5.4 Grace initialization */
/* Author: pcwebmaster (Arctic Wolf) */
/* Function: */
/* system_init */
/* com_send_command */
/* com_command_receive */
/* CalCRC16_1021 */
/* command_decoder */
/* send_command */
/* Test: Send: 16 16 02 01 02 01 00 35 03 94 BD Receive: 49 AA 15 */
/* Test phenomenon: */
/* Historical record: 20016.02.23 test passed */
/* Arctic Wolf 20016-02-23 Creat Inital version. (Version 1.0) */
/********************************************************************************/
#include <msp430.h>
#include <ti/mcu/msp430/Grace.h>
#include "comint.h"
uint8_t pint_buf[MAX_RINTL]; /* Serial port receive buffer */
uint8_t pint_read; /* Serial port buffer read pointer */
uint8_t pint_write; /* Serial port buffer write pointer */
//uint8_t psend_int; /* Serial port send permission flag */
uint8_t serial_flag = 0; /* Serial port receive data flag */
uint8_t prec_buf[MAX_COMMAND_LEN]; /* Command receiving buffer */
uint8_t prec_num; /* Number of bytes in command receiving buffer */
uint8_t serial_lengthl = 0; /* Message command length lower 8 bits */
uint16_t serial_length = 0; /* Message command length 16 bits */
uint8_t ADDRESS[2]={ZU,ZHAN}; /* byte0: communication group address, byte1: station address*/
//------------------------------------------------ ------------------
// Serial port initialization
//-------------------------------- -------------------------------------
void USCI_A0_graceInit(void)
{
/* USER CODE START ( section: USCI_A0_graceInit_prologue) */
/* User initialization code */
/* USER CODE END (section: USCI_A0_graceInit_prologue) */
/* Disable USCI */
UCA0CTL1 |= UCSWRST;
/*
* Control Register 1
*
* UCSSEL_2 -- SMCLK
* ~UCRXEIE -- Erroneous characters rejected and UCAxRXIFG is not set
* ~UCBRKIE -- Received break characters do not set UCAxRXIFG
* ~UCDORM -- Not dormant. All received characters will set UCAxRXIFG
* ~UCTXADDR -- Next frame transmitted is data
* ~UCTXBRK -- Next frame transmitted is not a break
* UCSWRST -- Enabled. USCI logic held in reset state
*
* Note: ~<BIT> indicates that <BIT> has value zero
*/
UCA0CTL1 = UCSSEL_2 | UCSWRST;
/*
* Modulation Control Register
*
* UCBRF_0 -- First stage 0
* UCBRS_1 -- Second stage 1
* ~UCOS16 -- Disabled
*
* Note: ~UCOS16 indicates that UCOS16 has value zero
*/
UCA0MCTL = UCBRF_0 | UCBRS_1;
/* Baud rate control register 0 */
UCA0BR0 = 104;
/* Enable USCI */
UCA0CTL1 &= ~UCSWRST;
/* USER CODE START (section: USCI_A0_graceInit_epilogue) */
/* User code */
/* USER CODE END (section: USCI_A0_graceInit_epilogue) */
}
//-----------------------------------------------------------------
// Serial port sends one byte
//-----------------------------------------------------------------
void com_send_command(char Onebyte)
{
UCA0TXBUF = Onebyte;
__delay_cycles(2000);//Send Chinese character parameter 2000, send character parameter is 1000
}
//------------------------------------------------------------------
// Serial port receive interrupt program
//------------------------------------------------------------------
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR_HOOK(void)
{
uint8_t temp;
uint8_t temp1;
// USER CODE START (section: USCI0RX_ISR_HOOK)
// replace this comment with your code
// USER CODE END (section: USCI0RX_ISR_HOOK)
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
//Judge whether the serial port is sending or not.
//Because we need to use the sending function, we have to judge whether the sending is busy.
//Only when it is not busy can we enable sending.
//This sentence is to judge whether the sending is working. If it is working, we will wait until it stops working.
temp1 = UCA0RXBUF; //Read data // TX -> RXed character
temp = pint_write + 1; /* Determine whether it can be written*/
if (temp == MAX_RINTL)
{
temp=0;
}
if (temp != pint_read)
{
pint_buf[pint_write] = temp1; /* Read data*/
pint_write = temp;
}
}
void buff_init(void)
{
uint8_t loop;
loop = UCA0RXBUF; /* Clear serial port buffer */
for (loop=0; loop<MAX_RINTL; loop++)
{
pint_buf[loop] = 0;
}
}
Comint.h
#ifndef __COMINT_H__
#define __COMINT_H__
#include <stdio.h>
#include <msp430.h>
#define INT8U unsigned char
#define INT16U unsigned int
#define uint8_t unsigned char
#define u_short unsigned int
#define uint16_t unsigned int
#define ZU 0x01 /*Group address*///Communication address Modify these two items
#define ZHAN 0x02 /*Station address*///Communication address Modify these two items
#define MAX_RINTL 16 /* Serial port receive buffer length */
#define SYN 0x16 /* Communication synchronization bit */
#define STX 0x02 /* Communication start bit */
#define ETX 0x03 /* Communication end bit */
#define ACK 0x06
#define NAK 0x15
#define MSG_ACK 2 /* Correct response information */
#define MSG_NAK 3 /* Error response information */
#define MAX_COMMAND_LEN 16 /* Length of the command received by the serial port */
extern uint8_t pint_read; //Serial port buffer read pointer */
extern uint8_t pint_write; //Serial port buffer write pointerextern
uint8_t pint_buf[MAX_RINTL]; //Serial port receive bufferextern
uint8_t serial_flag; /* Serial port receive data flag */
extern uint8_t prec_buf[MAX_COMMAND_LEN];/* Command receiving buffer*/
/* Serial port initialization*/
void Serial_port_init(void);
/* Serial port sends one byte*/
void com_send_command(char Onebyte);
void Send_String(char *str);// Send string to PC
/* Serial port receive data processing*/
void com_command_receive(void);
/* Serial port receive initialization*/
void buff_init(void);
///* Receive one byte of data from the serial port*/
//unsigned char UartReadChar(void); //reentrant
/*Calculate CRC checksum using MTT(0X1021)
parameters:
pBuff is the head pointer of the buffer for which CRC needs to be calculated
BufferLen is the buffer length (in bytes)
*/
u_short CalCRC16_1021(uint8_t x[], u_short BufferLen);
/* Command decoding subroutine*/
void command_decoder(void);
/* Send message frame to the host, input parameter: message type*/
void send_command(uint8_t command);
#endif
|