📄 lab2.c
字号:
/*
In Lab 2 the user has to configure the ADC 10 for a simple ADC conversion
The ADC steps are:
1. Manual trigger of ADC conversion using the SAMP bit
2. Manual sampling using the DelayNmSec(N) routine
3. The Pot marked REF and connected to RB2/AN2 is converted
This program uses the c compiler library functions to configure and
use the UART1
In this program, the UART1 is configured for a 9600 baud rate.
The result can be viewed using a serial connection to Hyperterm
*/
#define __dsPIC30F2010__
#include "C:\pic30_tools\support\h\uart.h"
#include "c:\pic30_tools\support\h\p30F2010.h"
#define BAUDRATE 9600 // desired baud rate
#define FCY 5000000 // xtal = 5.0Mhz; PLLx4
#define MILLISEC FCY/10000 // 1 mSec delay constant
#define LF 0x0A
#define CR 0x0D
#define NULL 0x00
#define SPACE 0x20
void InitADC10(void);
void LoadADC(void);
void InitUART1(void);
void DelayNmSec(unsigned int N);
int ADCValue;
char ADCdata[] = {'P','o','t',SPACE,'R','e','f','=',
'0','x','A','D','C',LF,CR,NULL};
#define VALUE 10 // Location of value in array above
int main(void)
{
unsigned char TxIndex;
InitADC10();
InitUART1();
while(1) // do for ever
{
DelayNmSec(100); // delay for 100 m Secs
TxIndex = 0; // point to first array element
/*******************************************************************/
ADCON1bits.SAMP = 1; // start sampling ...
DelayNmSec(100); // for 100 mS
ADCON1bits.SAMP = 0; // start Converting
while (!IFS0bits.ADIF); // wait till conversion done
IFS0bits.ADIF = 0; // clear ADIF if done
ADCValue = ADCBUF0; // get ADC value
LoadADC(); // Load xmit array
while (ADCdata[TxIndex]) // and transmit
{
WriteUART1((int)ADCdata[TxIndex++]);
while(BusyUART1());
}
} // end of while (1)
}
/*******************************************************************
Below is the code required to setup the ADC registers for :
1. 1 channel conversion (in this case RB2/AN2)
2. Manual Sample start
3. User specified sampling delay (100mS in this case)
4. Manual Stop Sampling and start converting
5. Manual check of Conversion complete
The code is as per Figure 18-3 in the Ref. manual
*********************************************************************/
void InitADC10(void)
{
ADPCFG = 0xFFFB; // all PORTB = Digital; RB2 = analog
ADCON1 = 0x0000; // SAMP bit = 0 ends sampling ...
// and starts converting
ADCHS = 0x0002; // Connect RB2/AN2 as CH0 input ..
// in this example RB2/AN2 is the input
ADCON3 = 0x0080; // Tad = internal RC (4uS)
ADCON1bits.ADON = 1; // turn ADC ON
}
/********************************************************************
LoadADC() converts the 10-bit value sampled on the REF pot (RB2)
to a 3 digit BCD value and loads it into the ADCdata xmit array
*********************************************************************/
void LoadADC(void)
{
unsigned char ADCbcd;
while(BusyUART1()); // wait till transmit is over
ADCbcd = (char)(ADCValue >> 8); // convert the ADC value
if (ADCbcd > 9)
ADCbcd = ADCbcd + 0x37;
else ADCbcd = ADCbcd + 0x30;
ADCdata[VALUE] = ADCbcd;
ADCbcd = (char)(ADCValue >> 4);
ADCbcd = ADCbcd & 0x0F;
if (ADCbcd > 9)
ADCbcd = ADCbcd + 0x37;
else ADCbcd = ADCbcd + 0x30;
ADCdata[VALUE+1] = ADCbcd;
ADCbcd = (char)ADCValue & 0x0F;
if (ADCbcd > 9)
ADCbcd = ADCbcd + 0x37;
else ADCbcd = ADCbcd + 0x30;
ADCdata[VALUE+2] = ADCbcd;
}
/*********************************************************************
The InitUART1 routine initializes the UART1 to 9600 baud using the
UART C function libraries.
*********************************************************************/
void InitUART1(void)
{
unsigned int baudvalue;
unsigned int U1MODEvalue;
unsigned int U1STAvalue;
CloseUART1();
ConfigIntUART1(UART_RX_INT_DIS & UART_RX_INT_PR6 &
UART_TX_INT_DIS & UART_TX_INT_PR2);
baudvalue = ((FCY/16)/BAUDRATE) - 1;
U1MODEvalue = UART_EN & UART_IDLE_CON & UART_RX_TX &
UART_DIS_WAKE & UART_DIS_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_3_4_FUL &
UART_ADR_DETECT_DIS &
UART_RX_OVERRUN_CLEAR;
OpenUART1(U1MODEvalue, U1STAvalue, baudvalue);
}
//---------------------------------------------------------------------
// This is a generic 1ms delay routine to give a 1mS to 65.5 Seconds delay
// For N = 1 the delay is 1 mS, for N = 65535 the delay is 65,535 mS.
// Note that FCY is used in the computation. Please make the necessary
// Changes(PLLx4 or PLLx8 etc) to compute the right FCY as in the define
// statement above.
void DelayNmSec(unsigned int N)
{
unsigned int j;
while(N--)
for(j=0;j < MILLISEC;j++);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -