David Buck
/
voltmeter
Revision 0:d322a5241da8, committed 2009-12-16
- Comitter:
- Kaikestu
- Date:
- Wed Dec 16 12:04:23 2009 +0000
- Commit message:
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ADC_setup.cpp Wed Dec 16 12:04:23 2009 +0000 @@ -0,0 +1,48 @@ +// ADC_setup.cpp + +#include "header.h" +/***************************************************** + variables and defines + ****************************************************/ + +int ADC_channel=1; // ADC channel 1 + +/***************************************************** + subroutines +*****************************************************/ +void ADC_init(){ + + PINSEL1=0x00010000; //set AD0.1 pin to 0000 0000 0000 0001 0000 0000 0000 0000 = ADC + PCONP |= (1 << 12); // enable ADC clock + AD0CR = (1 << ADC_channel) // channel 1 + | (4 << 8) // This gives us 3MHz + | (0 << 16) // BURST = 0, software control + | (0 << 17) // CLKS = 0 (11-bits) + | (1 << 21) // PDN = 1 + | (0 << 22) // TEST1:0 = 00 + | (1 << 24) // START = 1 start A/D conversion + | (0 << 27); // EDGE = 0 (CAP/MAT singal falling,trigger A/D conversion) +} +float ADC_read() { + + int data; + + // start A/D conversion on channel + AD0CR &= (AD0CR & 0xFFFFFF00); + AD0CR |= (1 << ADC_channel) | (1 << 24); + + delay_l(); + + // wait for it to finish by polling the ADC DONE bit + while((AD0GDR & 0x80000000) == 0) + { + } + + // get the data and stop the adc + data = AD0GDR; + AD0CR &= 0xF8FFFFFF; + + // shift data and mask to give 8-bit ADC value + data=(data>>8)&0x00000FF; + return data; + } \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LCD_setup.cpp Wed Dec 16 12:04:23 2009 +0000 @@ -0,0 +1,113 @@ +// LCD_setup.cpp + +#include "header.h" +/***************************************************** + subroutines +*****************************************************/ +/****initialise LCD function ****/ +void LCD_init(void){ + delay_l(); + FIO2DIR0=0xFF; /* set port 2 byte 0 (ctrl) to output */ + FIO2PIN0=0x00; //clear Port 2 byte 0 + //FIO2PIN bit0=R/W (read/write) + //FIO2PIN bit1=RS (register select) + //FIO2PIN bit2=E (enable) + //all initialised low + + // set Port 0 and 1 to operate as fast registers + SCS |= 0x00000001; // by setting the GPIOM bit of the SCS register + FIO0DIR2=0xFF; // set port 0 byte 2 (data) to output + FIO0PIN2=0x00; // clear Port 0 byte 2 + + /* RS already initialised to 'instruction' (low) */ + + /**** Function set*/ + FIO0PIN2=0x38; /* 2-line mode, 7 dot characters */ + toggle_enable(); + + /**** Display on/off*/ + FIO0PIN2=0x0C; /* display on, cursor off, blink off */ + toggle_enable(); + + /**** Clear display */ + FIO0PIN2=0x01; /* clear */ + toggle_enable(); + + /**** Set entry mode */ + FIO0PIN2=0x06; /* increment cursor, shift off */ + toggle_enable(); + +} + + +/**** LCD Display 'Voltmeter' */ +void LCD_displayV(){ + FIO2CLR0=0x02; /* set RS to 'control' */ + FIO0PIN2=0x01; /* clear display*/ + toggle_enable(); + FIO2SET0=0x02; /* set RS to 'data' */ + + FIO0PIN2=_sp; // process and display character + toggle_enable(); + FIO0PIN2=_sp; + toggle_enable(); + FIO0PIN2=_sp; + toggle_enable(); + FIO0PIN2=_V; + toggle_enable(); + FIO0PIN2=_O; + toggle_enable(); + FIO0PIN2=_L; + toggle_enable(); + FIO0PIN2=_T; + toggle_enable(); + FIO0PIN2=_M; + toggle_enable(); + FIO0PIN2=_E; + toggle_enable(); + FIO0PIN2=_T; + toggle_enable(); + FIO0PIN2=_E; + toggle_enable(); + FIO0PIN2=_R; + toggle_enable(); + + /* start of line 2 */ + FIO2CLR0=0x02; /* set RS to 'instruction' */ + FIO0PIN2=0xC6; /* set address to line 2 */ + toggle_enable(); + FIO2SET0=0x02; /* set RS to 'data' */ + + FIO0PIN2=_0; + toggle_enable(); + FIO0PIN2=0x2e; + toggle_enable(); + FIO0PIN2=_0; + toggle_enable(); + FIO0PIN2=_V; + toggle_enable(); +} + +/*** toggle_enable */ +void toggle_enable(void){ + FIO2SET0=0x04; // Toggle enable flag on */ + delay_s(); + FIO2CLR0=0x04; /* Toggle enable flag off */ + delay_s(); +} + +/************************************************************************** +* DEM16217 LCD connected to mbed as follows: +************************************************************************** +RW = Read/Write = LCD pin05 = mbed pin16 = LPC pin75 = GPIO P2.0 (Port2,byte0,bit0) +RS = RegisterSelect = LCD pin04 = mbed pin15 = LPC pin74 = GPIO P2.1 (0,1) +E = Enable = LCD pin06 = mbed pin14 = LPC pin73 = GPIO P2.2 (0,2) +DB0 = Data Bit 0 = LCD pin07 = mbed pin10 = LPC pin63 = GPIO P0.16 (Port0,byte2,bit0) +DB1 = Data Bit 1 = LCD pin08 = mbed pin08 = LPC pin61 = GPIO P0.17 (2,1) +DB2 = Data Bit 2 = LCD pin09 = mbed pin07 = LPC pin60 = GPIO P0.18 (2,2) +DB3 = Data Bit 3 = LCD pin10 = mbed pin06 = LPC pin59 = GPIO P0.19 (2,3) +DB4 = Data Bit 4 = LCD pin11 = mbed pin05 = LPC pin58 = GPIO P0.20 (2,4) +DB5 = Data Bit 5 = LCD pin12 = mbed pin04 = LPC pin57 = GPIO P0.21 (2,5) +DB6 = Data Bit 6 = LCD pin13 = mbed pin03 = LPC pin56 = GPIO P0.22 (2,6) +DB7 = Data Bit 7 = LCD pin14 = mbed pin36 = LPC pin09 = GPIO P0.23 (2,7) +**************************************************************************/ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/header.h Wed Dec 16 12:04:23 2009 +0000 @@ -0,0 +1,125 @@ +/* header.h header file */ + +/*******************************************************/ +/* function prototypes */ +/*******************************************************/ + +/* main.cpp prototypes */ + +/* ADC_setup.cpp prototypes */ +void ADC_init(void); +float ADC_read(void); + +/* LDC_setup.cpp prototypes */ +void LCD_init(void); +void LCD_displayV(void); +void toggle_enable(void); + +/* mbed_functions.cpp prototypes*/ +void delay_s(void); +void delay_l(void); + +/********************************************************/ +/* #defines */ +/********************************************************/ + +// *** mc9s12dp256 register addresses + + // Pin Assignment + #define PINSEL_BASE_ADDR 0xE002C000 + //PINSEL1 + #define PINSEL1 (*(volatile unsigned long *)(PINSEL_BASE_ADDR + 0x04)) + + //SCS - System Controls and Status, & Power Control */ + #define SCB_BASE_ADDR 0xE01FC000 + #define SCS (*(volatile unsigned long *)(SCB_BASE_ADDR + 0x1A0)) + #define PCONP (*(volatile unsigned long *)(SCB_BASE_ADDR + 0x0C4)) + + // Digital IO registers + #define FIO_BASE_ADDR 0x3FFFC000 + //FIO2DIR0 + #define FIO2DIR0 (*(volatile unsigned char *)(FIO_BASE_ADDR + 0x40)) + //FIO2PIN0 + #define FIO2PIN0 (*(volatile unsigned char *)(FIO_BASE_ADDR + 0x54)) + //FIO2CLR0 + #define FIO2CLR0 (*(volatile unsigned char *)(FIO_BASE_ADDR + 0x5C)) + //FIO2SET0 + #define FIO2SET0 (*(volatile unsigned char *)(FIO_BASE_ADDR + 0x58)) + //FIO0DIR2 + #define FIO0DIR2 (*(volatile unsigned char *)(FIO_BASE_ADDR + 0x02)) + //FIO0PIN2 + #define FIO0PIN2 (*(volatile unsigned char *)(FIO_BASE_ADDR + 0x16)) + + // ADC registers + #define AD0_BASE_ADDR 0xE0034000 + //AD0CR + #define AD0CR (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x00)) + //AD0GDR + #define AD0GDR (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x04)) + + +/* LCD display constants */ +#define _sp 0x20 /* space */ +#define _eq 0x3d /* = sign */ +#define _0 0x30 +#define _1 0x31 +#define _2 0x32 +#define _3 0x33 +#define _4 0x34 +#define _5 0x35 +#define _6 0x36 +#define _7 0x37 +#define _8 0x38 +#define _9 0x39 +#define _A 0x41 +#define _B 0x42 +#define _C 0x43 +#define _D 0x44 +#define _E 0x45 +#define _F 0x46 +#define _G 0x47 +#define _H 0x48 +#define _I 0x49 +#define _J 0x4a +#define _K 0x4b +#define _L 0x4c +#define _M 0x4d +#define _N 0x4e +#define _O 0x4f +#define _P 0x50 +#define _Q 0x51 +#define _R 0x52 +#define _S 0x53 +#define _T 0x54 +#define _U 0x55 +#define _V 0x56 +#define _W 0x57 +#define _X 0x58 +#define _Y 0x59 +#define _Z 0x5a +#define _a 0x61 +#define _b 0x62 +#define _c 0x63 +#define _d 0x64 +#define _e 0x65 +#define _f 0x66 +#define _g 0x67 +#define _h 0x68 +#define _i 0x69 +#define _j 0x6a +#define _k 0x6b +#define _l 0x6c +#define _m 0x6d +#define _n 0x6e +#define _o 0x6f +#define _p 0x70 +#define _q 0x71 +#define _r 0x72 +#define _s 0x73 +#define _t 0x74 +#define _u 0x75 +#define _v 0x76 +#define _w 0x77 +#define _x 0x78 +#define _y 0x79 +#define _z 0x7a \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Dec 16 12:04:23 2009 +0000 @@ -0,0 +1,122 @@ +//main.cpp +/**************************************************************** +* Include files +****************************************************************/ +#include "header.h" + +/**************************************************************** +* data values +****************************************************************/ +#define cal -200 // calibration value + +int A2D_in; /* input variable */ +int V_in; /* input voltage */ +int V_unit; /* voltage units value */ +int V_unit_old; // previous unit value +int V_in_dec; /* input voltage decimal value */ +int V_dec; /* voltage decimal value */ +int V_dec_old; // previous decimal value +float x; + +/**************************************************************** +* Main part of the program +****************************************************************/ +int main() +{ + + ADC_init(); + LCD_init(); + LCD_displayV(); + +while(1){ /* infinite loop */ + + A2D_in=ADC_read(); + V_in=A2D_in*33; /* calculate voltage from 8-bit ADC input 0xFF = 3.3V */ + // all voltage values multiplied by 10 to allow integer manipulation + + + //****************** calculate voltage unit = 0-5V ****************************** + if(V_in >= 50*255) // if greater than 5V then unit to display = 5 + { + V_unit = 0x05; + V_in_dec = 0; // if unit = 5 then decimal must be 0 (5.0V=max) + } + else if(V_in > 40*255) // else if greater than 4V then unit to display = 4 + { + V_unit = 0x04; + V_in_dec = V_in-40*256; // calculate remainder for decimal processing + } + else if(V_in > 30*255) // else if greater than 3V then unit to display = 3 + { + V_unit = 0x03; + V_in_dec = V_in-30*256; + } + else if(V_in > 20*255) // else if greater than 2V then unit to display = 2 + { + V_unit = 0x02; + V_in_dec = V_in-20*256; + } + else if(V_in > 10*255) // else if greater than 1V then unit to display = 1 + { + V_unit = 0x01; + V_in_dec = V_in-10*256; + } + else // else unit to display = 0 + { + V_unit = 0x00; + V_in_dec = V_in; + } + + // if unit has changed, then display the new unit on the screen + if (V_unit!= V_unit_old){ + + // start of line 2 + FIO2CLR0=0x02; /* set RS to 'instruction' */ + FIO0PIN2=0xC6; /* set address to line 2 units position*/ + toggle_enable(); + FIO2SET0=0x02; /* set RS to 'data' */ + + FIO0PIN2=(0x30+V_unit); // display unit value on screen + toggle_enable(); + V_unit_old=V_unit; + } + + + //****************** calculate decimal value = .0 - .9 ****************************** + if(V_in_dec <=260+cal) + V_dec = 0x00; + if(V_in_dec > 260+cal) // decimal value must be between 0 and 2550 (ie 255*10) + V_dec = 0x01; + if(V_in_dec > 510+cal) // calibration value allows an amount of simple tuning + V_dec = 0x02; // any error should be linear, but maybe with an offset + if(V_in_dec > 770+cal) // a complete look-up table would be better though! + V_dec = 0x03; + if(V_in_dec > 1120+cal) + V_dec = 0x04; + if(V_in_dec > 1280+cal) + V_dec = 0x05; + if(V_in_dec > 1530+cal) + V_dec = 0x06; + if(V_in_dec > 1780+cal) + V_dec = 0x07; + if(V_in_dec > 2040+cal) + V_dec = 0x08; + if(V_in_dec > 2300+cal) + V_dec = 0x09; + + // if decimal value has changed then update display + if (V_dec!= V_dec_old){ + + // start of line 2 */ + FIO2CLR0=0x02; /* set RS to 'instruction' */ + FIO0PIN2=0xC8; /* set address to line 2 */ + toggle_enable(); + FIO2SET0=0x02; /* set RS to 'data' */ + + FIO0PIN2=(0x30+V_dec); + toggle_enable(); + V_dec_old=V_dec; + } + } + +} /* end main() */ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Dec 16 12:04:23 2009 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/00a04e5cd407
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_functions.cpp Wed Dec 16 12:04:23 2009 +0000 @@ -0,0 +1,23 @@ +// mbed_functions.cpp + +#include "header.h" +/***************************************************** + subroutines - commonly used functions +*****************************************************/ + +/*** short delay */ +void delay_s(void){ + int j; /* loop variable */ + + for(j=0;j<2000;j++){ + asm("NOP"); + } +} +/*** long delay */ +void delay_l(void){ + int i; /* loop variable */ + + for(i=0;i<500;i++){ + delay_s(); + } +} \ No newline at end of file