#include #include #include "c8051f350.h" #define PULLUP1 P1_4 // 10k pullup resistor on channel 1 (0=ON, 1=OFF) #define PULLDN1 P1_5 // 10k pulldown resistor on channel 1 (0=OFF, 1=ON) #define PULLUP2 P0_0 // 10k pullup resistor on channel 2 (0=ON, 1=OFF) #define PULLDN2 P0_1 // 10k pulldown resistor on channel 2 (0=OFF, 1=ON) #define LED P0_6 // on board LED unsigned char BinaryData=0; // Peripheral specific initialization functions, // Called from the Init_Device() function void PCA_Init() { PCA0MD &= ~0x40; // disable watchdog timer PCA0MD = 0x00; } void Timer_Init() { TCON = 0x40; // enable Timer 1 TMOD = 0x20; // Timer 1 mode is 8-bit auto reload CKCON = 0x08; // Timer 1 timebase is system clock (24,5MHz) TH1 = 0xCB; // configure timers for 230400 bit/s UART data rate } void UART_Init() { SCON0 = 0x10; // enable UART for 8-bit, variable baud mode TI=1; // set this flag to indicate empty output buffer } // Configure the ADC: // 10Hz sample rate, unipolar mode // gain=1, internal voltage reference // analog input buffers are bypassed // ADC input is Channel 2 and Channel 1 void ADC_Init() { ADC0MD = 0x80; // disable ADC, will be enabled later ADC0CN = 0x10; ADC0CLK = 0x09; // 10Hz sample rate ADC0DECH = 0x07; // 10Hz sample rate ADC0DECL = 0x79; // 10Hz sample rate ADC0MUX = 0x32; // default ADC multiplexer selection is ADC positive=Input1 and ADC negative=Input2 } void Voltage_Reference_Init() { REF0CN = 0x03; // enable internal reference and bias generator } void Port_IO_Init() { // P0.0 - Unassigned, Open-Drain, Digital // P0.1 - Unassigned, Open-Drain, Digital // P0.2 - Unassigned, Open-Drain, Digital // P0.3 - Unassigned, Open-Drain, Digital // P0.4 - TX0 (UART0), Push-Pull, Digital // P0.5 - RX0 (UART0), Open-Drain, Digital // P0.6 - Unassigned, Push-Pull, Digital // P0.7 - Unassigned, Open-Drain, Digital // P1.0 - Unassigned, Open-Drain, Digital // P1.1 - Unassigned, Open-Drain, Digital // P1.2 - Unassigned, Open-Drain, Digital // P1.3 - Unassigned, Open-Drain, Digital // P1.4 - Unassigned, Open-Drain, Digital // P1.5 - Unassigned, Open-Drain, Digital // P1.6 - Unassigned, Open-Drain, Digital // P1.7 - Unassigned, Open-Drain, Digital P0MDOUT = 0x50; XBR0 = 0x01; XBR1 = 0x40; } void Oscillator_Init() { OSCICN = 0x83; // internal oscillator, 24.5MHz } // Initialization function for device, void Init_Device(void) { PCA_Init(); Timer_Init(); UART_Init(); ADC_Init(); Voltage_Reference_Init(); Port_IO_Init(); Oscillator_Init(); } /************************************************************************** waits for the specified time ***************************************************************************/ void Delay_ms(unsigned int ms) { unsigned int i; for(i=0; i'7') c='7'; ADC0CN = (ADC0CN & 0xF8) | ((c-'0') & 0x7); } else if (c=='1') // input 1, switch pullup and down resistors on or off { c=SInOut(); if (c=='U') PULLUP1 = 0; // switch pullup on else if (c=='u') PULLUP1 = 1; // switch pullup off else if (c=='D') PULLDN1 = 1; // switch pulldown on else if (c=='d') PULLDN1 = 0; // switch pulldown off } else if (c=='2') // input 2, switch pullup and down resistors on or off { c=SInOut(); if (c=='U') PULLUP2 = 0; // switch pullup on else if (c=='u') PULLUP2 = 1; // switch pullup off else if (c=='D') PULLDN2 = 1; // switch pulldown on else if (c=='d') PULLDN2 = 0; // switch pulldown off } else if (c=='p') // get port bits { if (BinaryData) { SOut((P0 >> 7) | ((P1 & 0x0F) << 1)); } else { SOut((P0_7) ? '1':'0'); SOut((P1_0) ? '1':'0'); SOut((P1_1) ? '1':'0'); SOut((P1_2) ? '1':'0'); SOut((P1_3) ? '1':'0'); } } else if (c=='P') // get port bits { if (BinaryData) { c=SInOut(); P0_7 = c & 1; P1_0 = c & 2; P1_1 = c & 4; P1_2 = c & 8; P1_3 = c & 16; } else { P0_7 = SInOut() & 1; P1_0 = SInOut() & 1; P1_1 = SInOut() & 1; P1_2 = SInOut() & 1; P1_3 = SInOut() & 1; } } else if (c=='x') // write ADC configuration registers directly { ADC0CN = SInOut(); ADC0CF = SInOut(); ADC0CLK = SInOut(); ADC0DECH = SInOut(); ADC0DECL = SInOut(); ADC0DAC = SInOut(); ADC0BUF = SInOut(); ADC0MUX = SInOut(); } else if (c=='X') // configure DACs and GPIO { IDA0CN = (SInOut()=='0') ? 0x72:0xF2; IDA1CN = (SInOut()=='0') ? 0x72:0xF2; c=((SInOut()-'0') & 1) << 4; c=c | ((SInOut()-'0') & 0x0F); P0MDOUT = (P1MDOUT & 0x80) | (c << 7); P1MDOUT = (P1MDOUT & 0x0F) | (c >> 1); } } }