?? canadc.c
字號:
#pragma REGPARMS
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "c8051f020.h"
#include "can20.h"
sfr16 DP = 0x82; // data pointer
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 ADC0 = 0xbe; // ADC0 data
sfr16 ADC0GT = 0xc4; // ADC0 greater than window
sfr16 ADC0LT = 0xc6; // ADC0 less than window
sfr16 RCAP2 = 0xca; // Timer2 capture/reload
sfr16 T2 = 0xcc; // Timer2
sfr16 RCAP4 = 0xe4; // Timer4 capture/reload
sfr16 T4 = 0xf4; // Timer4
sfr16 DAC0 = 0xd2; // DAC0 data
sfr16 DAC1 = 0xd5; // DAC1 data
#define BAUDRATE 115200 // Baud rate of UART in bps
#define SYSCLK 22118400 // SYSCLK frequency in Hz
#define SAMPLE_RATE 50000 // Sample frequency in Hz
#define INT_DEC 256 // integrate and decimate ratio
unsigned char xdata SendBuffer[13];
unsigned char xdata GetBuffer[13];
long result;
void config (void) {
//Local Variable Definitions
WDTCN = 0x07; // Watchdog Timer Control Register
WDTCN = 0xDE; // Disable WDT
WDTCN = 0xAD;
XBR0 = 0xEF; // XBAR0: Initial Reset Value
XBR1 = 0x07; // XBAR1: Initial Reset Value
XBR2 = 0x44; // XBAR2: Initial Reset Value
// Port configuration (1 = Push Pull Output)
P0MDOUT = 0x1D; // Output configuration for P0
P1MDOUT = 0x01; // Output configuration for P1
P2MDOUT = 0x03; // Output configuration for P2
P3MDOUT = 0x00; // Output configuration for P3
P74OUT = 0xff; // Output configuration for P4-7
P1MDIN = 0xFF; // Input configuration for P1
// View port pinout
// The current Crossbar configuration results in the
// following port pinout assignment:
// Port 0
// P0.0 = UART TX0 (Push-Pull Output)
// P0.1 = UART RX0 (Open-Drain Output/Input)
// P0.2 = SPI Bus SCK (Push-Pull Output)
// P0.3 = SPI Bus MISO (Push-Pull Output)
// P0.4 = SPI Bus MOSI (Push-Pull Output)
// P0.5 = SPI Bus NSS (Open-Drain Output/Input)
// P0.6 = SMBus SDA (Open-Drain Output/Input)
// P0.7 = SMBus SCL (Open-Drain Output/Input)
// Port 1
// P1.0 = UART TX1 (Push-Pull Output)(Digital)
// P1.1 = UART RX1 (Open-Drain Output/Input)(Digital)
// P1.2 = PCA CEX0 (Open-Drain Output/Input)(Digital)
// P1.3 = PCA CEX1 (Open-Drain Output/Input)(Digital)
// P1.4 = PCA CEX2 (Open-Drain Output/Input)(Digital)
// P1.5 = PCA CEX3 (Open-Drain Output/Input)(Digital)
// P1.6 = PCA CEX4 (Open-Drain Output/Input)(Digital)
// P1.7 = PCA ECI (Open-Drain Output/Input)(Digital)
// Port 2
// P2.0 = CP0 (Push-Pull Output)
// P2.1 = CP1 (Push-Pull Output)
// P2.2 = T0 (Open-Drain Output/Input)
// P2.3 = /INT0 (Open-Drain Output/Input)
// P2.4 = GP I/O (Open-Drain Output/Input)
// P2.5 = GP I/O (Open-Drain Output/Input)
// P2.6 = GP I/O (Open-Drain Output/Input)
// P2.7 = GP I/O (Open-Drain Output/Input)
// Port 3
// P3.0 = GP I/O (Open-Drain Output/Input)
// P3.1 = GP I/O (Open-Drain Output/Input)
// P3.2 = GP I/O (Open-Drain Output/Input)
// P3.3 = GP I/O (Open-Drain Output/Input)
// P3.4 = GP I/O (Open-Drain Output/Input)
// P3.5 = GP I/O (Open-Drain Output/Input)
// P3.6 = GP I/O (Open-Drain Output/Input)
// P3.7 = GP I/O (Open-Drain Output/Input)
EMI0CF = 0x24; // External Memory Configuration Register
EMI0CN=0x00;
EMI0TC=0x01;
}
void SYSCLK_Init (void)
{
int xdata i; // delay counter
OSCXCN = 0x67; // start external oscillator with
// 22.1184MHz crystal
for (i=0; i < 256; i++) ; // XTLVLD blanking interval (>1ms)
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x88; // select external oscillator as SYSCLK
// source and enable missing clock
// detector
}
void UART0_Init (void)
{
SCON0 = 0x50; // SCON0: T1 mode 1, 8-bit UART, enable RX
TMOD = 0x21; // TMOD: timer 1, mode 2, 8-bit reload
// T0 16 bit Timer
T2CON &= 0x0cf; //select T1 as baudrate
TH1 = -(SYSCLK/BAUDRATE/16); // set Timer1 reload value for baudrate
TR1 = 1; // start Timer1
CKCON |= 0x10; // Timer1 uses SYSCLK as time base
PCON |= 0x80; // SMOD00 = 1
// ES0=1;
}
unsigned char Can_Init(void)
{
unsigned char tt;
unsigned int i;
P3=0xff;
for(i=0;i<1000;i++);
P3=0x00;
for(i=0;i<1000;i++);
P3=0xff;
for(i=0;i<1000;i++);
ModeControlReg= RM_RR_Bit;
tt=ModeControlReg;
ClockDivideReg=0x88;
InterruptEnReg=0xff;
BusTiming0Reg= 0x03; //100 K
BusTiming1Reg=0x2f;
AcceptCode0Reg=0x57;
AcceptCode1Reg=0x00;
AcceptCode2Reg=0xaa;
AcceptCode3Reg=0x55;
AccepMask0Reg=0xff;
AccepMask1Reg=0xff;
AccepMask2Reg=0xff;
AccepMask3Reg=0xff;
RxBufstartAdr=0;
TxErrCountReg=0;
RxErrCountReg=0;
ErrCodeCapReg=0;
OutControlReg=0x1a;
ModeControlReg=AFM_Bit;
tt=StatusReg;
while(tt==0x10)
tt=StatusReg;
return tt;
}
unsigned char WriteCan(unsigned int a,unsigned int b)
{
unsigned char * xdata tt;
unsigned int i;
unsigned char tt1,count;
count=11;
tt1=StatusReg;
if((tt1&RS_Bit)!=0x00)return 0;
tt1=StatusReg;
if((tt1&TCS_Bit)==0x00)return 0;
tt=&TxFramInFo;
SendBuffer[0]=0x08;
SendBuffer[1]=0x01;
SendBuffer[2]=0x08;
SendBuffer[3]=a;
SendBuffer[4]=b;
SendBuffer[5]=0x0;
SendBuffer[6]=0x0;
SendBuffer[7]=0x0;
SendBuffer[8]=0x0;
SendBuffer[9]=0x0;
SendBuffer[10]=0x0;
for(i=0;i<count;i++)
{*tt=SendBuffer[i];
tt++;
}
CommandReg=TR_Bit;
return 1;
}
void INTE0_ISR (void) interrupt 0
{
unsigned char tt1;
unsigned char * xdata tt;
unsigned int i,count;
tt1=InterruptReg;
if((tt1&0x04)!=0)
{
tt1=StatusReg;
if((tt1&0x80)!=0)
ModeControlReg=AFM_Bit;
return;
}
if((tt1&0x08)!=0)
{CommandReg=0x0c;
return;
}
if((tt1&0x01)!=0)
{tt=&RxFramInFo;
count=(*tt&0x0f);
if((*tt&0x80)==0)
count+=3;
else
count+=5;
for(i=0;i<count;i++)
{GetBuffer[i]=*tt;
tt++;
}
CommandReg=RRB_Bit;
}
tt1=ArbLostCapReg;
tt1=ErrCodeCapReg;
return;
}
void ADC0_Init (void)
{
ADC0CN = 0x05; // ADC0 disabled; normal tracking
// mode; ADC0 conversions are initiated
// on overflow of Timer3; ADC0 data is
// left-justified
REF0CN = 0x07; // enable temp sensor, on-chip VREF,
// and VREF output buffer
AMX0SL = 0x0f; // Select TEMP sens as ADC mux output
ADC0CF = (SYSCLK/2500000) << 3; // ADC conversion clock = 2.5MHz
ADC0CF |= 0x01; // PGA gain = 2
EIE2 |= 0x02; // enable ADC interrupts
}
//-----------------------------------------------------------------------------
// Timer3_Init
//-----------------------------------------------------------------------------
//
// Configure Timer3 to auto-reload at interval specified by <counts> (no
// interrupt generated) using SYSCLK as its time base.
//
void Timer3_Init (int counts)
{
TMR3CN = 0x02; // Stop Timer3; Clear TF3;
// use SYSCLK as timebase
TMR3RL = -counts; // Init reload values
TMR3 = 0xffff; // set to reload immediately
EIE2 &= ~0x01; // disable Timer3 interrupts
TMR3CN |= 0x04; // start Timer3
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// ADC0_ISR
//-----------------------------------------------------------------------------
//
// ADC0 end-of-conversion ISR
// Here we take the ADC0 sample, add it to a running total <accumulator>, and
// decrement our local decimation counter <int_dec>. When <int_dec> reaches
// zero, we post the decimated result in the global variable <result>.
//
void ADC0_ISR (void) interrupt 15
{
static unsigned int_dec=INT_DEC; // integrate/decimate counter
// we post a new result when
// int_dec = 0
static long accumulator=0L; // here's where we integrate the
// ADC samples
AD0INT = 0; // clear ADC conversion complete
// indicator
accumulator += ADC0; // read ADC value and add to running
// total
int_dec--; // update decimation counter
if (int_dec == 0) { // if zero, then post result
int_dec = INT_DEC; // reset counter
result = accumulator >> 8;
accumulator = 0L; // reset accumulator
}
}
main()
{
unsigned long temperature; // temperature in hundredths of a
// degree C
unsigned int temp_int, temp_frac;
SYSCLK_Init();
config();
UART0_Init();
Can_Init();
Timer3_Init (SYSCLK/SAMPLE_RATE); // initialize Timer3 to overflow at
// sample rate
ADC0_Init (); // init ADC
AD0EN = 1; // enable ADC
EA=1;
EX0=1;
IP=0x01;
while(1)
{
temperature = result;
// calculate temperature in hundredths of a degree
temperature = temperature - 42380;
temperature = (temperature * 100L) / 156;
temp_int = temperature / 100;
temp_frac = temperature - (temp_int * 100);
if(WriteCan(temp_int,temp_frac)==0x01)
{SBUF0=0xAA;
while(TI0==0);
TI0=0;
SBUF0=0x55;
while(TI0==0);
TI0=0;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -