//------------------------------------------------------------------------------------
// F02x_Blinky.c
//------------------------------------------------------------------------------------
// Copyright (C) 2007 Silicon Laboratories, Inc.
//
// AUTH: BW
// DATE: 4 SEP 01
//
// This program flashes the green LED on the C8051F020 target board about five
times
// a second using the interrupt handler for Timer3.
// Target: C8051F02x
//
// Tool chain: KEIL Eval 'c'
//
//------------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------------
#include <c8051f020.h> // SFR declarations
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <intrins.h>
//------------------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F02x
//------------------------------------------------------------------------------------
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
//------------------------------------------------------------------------------------
// Global CONSTANTS
//------------------------------------------------------------------------------------
#define BAUDRATE 9600 // Baud rate of UART in bps
// 波特率必须是:9600 19200 38400 57600 115200
// 22118400频率太高,无法设置4800以下的波特率
// SYSTEMCLOCK = System clock frequency in Hz
#define SYSTEMCLOCK 22118400
#define SYSCLK 2000000 // approximate SYSCLK frequency in Hz
unsigned char xdata h[4096];
unsigned char a,b,a1,b1,d2;
float c,d,d1,f,f1,g[10];
// green LED: '1' = ON; '0' = OFF
sbit RS = P1^0;
sbit Q2 = P1^1;
sbit Q1 = P1^2;
sbit SH = P1^3;
unsigned long int i,j,k,m;
unsigned char xdata high[2048];
unsigned char xdata low[2048];
//------------------------------------------------------------------------------------
// Function PROTOTYPES
//------------------------------------------------------------------------------------
void CCD_PORT_Init (void);
void OSCILLATOR_Init (void);
void ADC0_Init (void);
void delay (unsigned long int time);
void UART0_Init (void);
void PORT1_Init (void);
//------------------------------------------------------------------------------------
// MAIN Routine
//------------------------------------------------------------------------------------
void main (void)
{
// disable watchdog timer
WDTCN = 0xde;
WDTCN = 0xad;
OSCILLATOR_Init (); // Initialize oscillator
CCD_PORT_Init ();
PORT1_Init ();
ADC0_Init ();
UART0_Init ();
P1 = 0x0d;
delay (15);
RS=0;
delay (15);
RS=1;
delay (15);
P1=0x0b;
//Q1=0;
// Q2=1;
delay (15);
RS=0;
delay (15);
RS=1;
delay (15);
m = 0;
while(1)
{
// for(m = 0; m < 10; m++)
// {
for(k = 0;k < 10; k++)
{
SH=0;
delay (1);
RS=0;
delay (1);
RS=1;
delay (1);
RS=0;
delay (1);
RS=1;
delay (1);
SH=1;
delay (1);
RS=0;
delay (1);
RS=1;
delay (1);
// Q1=1;
//Q2=0;
for(j = 0;j<20;j++)
{
P1=0x0d;
delay (1);
AD0BUSY = 1;
while(AD0INT == 0);
AD0BUSY = 0;
AD0INT = 0;
// high[m] = ADC0>>8;
// low[m] = ADC0&0xff;
RS=0;
delay (1);
RS=1;
delay (1);
// m++;
P1=0x0b;
delay (1);
AD0BUSY = 1;
while(AD0INT == 0);
AD0BUSY = 0;
AD0INT = 0;
// high[m] = ADC0>>8;
// low[m] = ADC0&0xff;
RS=0;
delay (1);
RS=1;
delay (1);
// m++;
}
for(j = 0;j<1024;j++)
{
P1=0x0d;
delay (1);
AD0BUSY = 1;
while(AD0INT == 0);
AD0BUSY = 0;
AD0INT = 0;
high[m] = ADC0>>8;
low[m] = ADC0&0x00ff;
RS=0;
delay (1);
RS=1;
delay (1);
m++;
P1=0x0b;
delay (1);
AD0BUSY = 1;
while(AD0INT == 0);
AD0BUSY = 0;
AD0INT = 0;
high[m] = ADC0>>8;
low[m] = ADC0&0x00ff;
RS=0;
delay (1);
RS=1;
delay (1);
m++;
}
for(j = 0;j<266;j++)
{
P1=0x0d;
delay (1);
AD0BUSY = 1;
while(AD0INT == 0);
AD0BUSY = 0;
AD0INT = 0;
// high[m] = ADC0>>8;
// low[m] = ADC0&0xff;
RS=0;
delay (1);
RS=1;
delay (1);
// m++;
P1=0x0b;
delay (1);
AD0BUSY = 1;
while(AD0INT == 0);
AD0BUSY = 0;
AD0INT = 0;
// high[m] = ADC0>>8;
// low[m] = ADC0&0xff;
RS=0;
delay (1);
RS=1;
delay (1);
// m++;
}
SH = 0;
m = 0;
}
for (i = 0; i<2048; i++)
{
SBUF0 = high[i];
while(TI0 == 0);
TI0 = 0;
SBUF0 = low[i];
while(TI0 == 0);
TI0 = 0;
}
TI0 = 0;
}
}
//------------------------------------------------------------------------------------
// PORT_Init
//------------------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports
//
void CCD_PORT_Init (void)
{
XBR2 = 0x40; // Enable crossbar and weak pull-ups
P1MDOUT = 0x0f; // enable P1.6 (LED) as push-pull output
}
//------------------------------------------------------------------------------------
// Timer3_Init
//------------------------------------------------------------------------------------
//
// Configure Timer3 to auto-reload and generate an interrupt at interval
// specified by <counts> using SYSCLK/12 as its time base.
//
void delay(unsigned long int time)
{
unsigned long int i;
for(i = 0;i < time;i++)
{
_nop_();
}
}
void OSCILLATOR_Init (void)
{
int i; // delay counter
OSCXCN = 0x67; // start external oscillator with
// 22.1184MHz crystal
for (i=0; i < 256; i++) ; // wait for oscillator to start
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x88; // select external oscillator as SYSCLK
// source and enable missing clock
// detector
}
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
REF0CN = 0x03; // Enable temp sensor, on-chip VREF,
ADC0CF = 0xF8; // PGA gain = 1 (default) // and VREF output buffer
AMX0CF = 0x00; // AIN inputs are single-ended (default)
AMX0SL = 0x00; // Select AIN0.1 pin as ADC mux input
// ADC0CF = 0x00; // ADC conversion clock = 2.5MHz
ADC0CN = 0x80; // ADC0 disabled; normal tracking
// mode; ADC0 conversions are initiated
// right-justified// on overflow of Timer3; ADC0 data is
}
void UART0_Init (void)
{
long int value;
value = 256 - (SYSTEMCLOCK/BAUDRATE)/16;
SCON0 = 0x50;
TMOD = 0x20;
TH1 = value;
// TH1 = 0x70;
TR1 = 1;
CKCON |= 0x10;
PCON |= 0x80;
}
void PORT1_Init (void)
{
XBR0 = 0x04;
XBR1 = 0x00;
XBR2 = 0x40; // Enable crossbar and weak pull-up
// Enable UART1
P0MDOUT |= 0x01; // Set TX pin to push-pull
}
一周热门 更多>