⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lab4.c

📁 dspic例程可以参考一下
💻 C
字号:
/*
This program uses the c compiler library functions to configure and
use the UART1 transmit function on the dsPICDEM 1.1 board.

The UART1 is configured for a 9600 baud rate. A "Microchip" message 
is transmitted everytime the SW1 key on the dsPICDEM1.1 is hit.
*/

#define __dsPIC30F6014__
#include <uart.h>
#include <p30F6014.h>
#include "LCD_display.h"

//---------------------------------------------------------------------------
// Configuration bits

   _FOSC(CSW_FSCM_OFF & XT_PLL4);   //XT with 4xPLL oscillator, Failsafe clock off
   _FWDT(WDT_OFF);                  //Watchdog timer disabled
   _FBORPOR(PBOR_OFF & MCLR_EN);    //Brown-out reset disabled, MCLR reset enabled
   _FGS(CODE_PROT_OFF);             //Code protect disabled

//---------------------------------------------------------------------------

#define BAUDRATE 9600		// desired baud rate
#define FCY  7372800			// xtal = 7.3728Mhz; PLLx4
#define SW1	!PORTAbits.RA12
#define SW2	!PORTAbits.RA13
#define LF	0x0A
#define CR	0x0D
#define NULL 0x00
#define WRITE_CHAR	0xA8
#define HOME_CLEAR	0x82
#define CURSOR_ON		0x8C

unsigned char RxValue;

void InitUART1(void);

// interrupt vector for UART1 TX interrupt
void __attribute__((__interrupt__)) _U1TXInterrupt(void)
{
	IFS0bits.U1TXIF = 0;	// clear interrupt flag
}

// interrupt vector for UART1 RX interrupt
void __attribute__((__interrupt__)) _U1RXInterrupt(void)
{
	IFS0bits.U1RXIF = 0;	// clear interrupt flag
	RxValue = (char)U1RXREG;	// read input register
	LCD_Display_Byte(WRITE_CHAR);	// display on LCD
	LCD_Display_Byte(RxValue);		// --do--
}

int main(void)
{
// char string = Microchip
char Txdata[] = {'M','i','c','r','o','c','h','i','p',CR,LF,NULL};
unsigned char TxIndex;

	InitUART1();			// Initialize UART1
	LCD_Display_Setup();	// Init LCD display
	LCD_Display_Byte(HOME_CLEAR);	// clear LCD and put cursor at home
	LCD_Display_Byte(CURSOR_ON); // turn on cursor
	TRISA = 0xFFFF;		// make RA all inputs for SW1-SW4
	while(1)					// repeat forever
	{
	TxIndex = 0;		// point to first char in string array
	while (!SW1); 		// start transmission when SW1 is pressed
	while (Txdata[TxIndex])	// do until NULL char is reached
		{
		WriteUART1((int)Txdata[TxIndex++]); //Call C peripheral library to transmit a character
		while(BusyUART1());	// wait if transmitter is busy
		}
	while (SW1);	// wait till SW1 is released
	}		// end of while forever

} // end of main program

void InitUART1(void)
{
	
unsigned int baudvalue;
unsigned int U1MODEvalue;
unsigned int U1STAvalue;

	CloseUART1();
	ConfigIntUART1(UART_RX_INT_EN & UART_RX_INT_PR6 & 
						UART_TX_INT_DIS & UART_TX_INT_PR2);
	U1MODEvalue = 	UART_EN & UART_IDLE_CON &
						UART_DIS_WAKE & UART_EN_LOOPBACK &
						UART_EN_ABAUD & UART_NO_PAR_8BIT &
						UART_1STOPBIT;
	U1STAvalue =	UART_INT_TX_BUF_EMPTY &
						UART_TX_PIN_NORMAL &
						UART_TX_ENABLE & UART_INT_RX_CHAR &
						UART_ADR_DETECT_DIS &
						UART_RX_OVERRUN_CLEAR;
// The BAUDRATE = 9600,  FCY is already defined so use 
// it as "FCY".  Plug into the formula provided in  the slides 
	baudvalue = ((FCY/16)/BAUDRATE) - 1; // initialize the variable baudvalue
	OpenUART1(U1MODEvalue, U1STAvalue, baudvalue);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -