Thread: Adc
View Single Post
Old Aug 12th, 2008, 1:17 AM   #1
musica07
Newbie
 
Join Date: Jun 2008
Posts: 11
Rep Power: 0 musica07 is on a distinguished road
Exclamation Adc

Hello.

I would like to ask for some help regarding the ADC. In my project, I'm using the 10-bit ADC. It has 1024 counts(0-1023).

I have a pressure sensor connected to pin RA0 of the PIC18F2620. I have to set an offset value of 1V where this value must correspond with count 310 of the ADC. It must also corresponds to 0mmHg.

What i have to do is to pump air into the cuff uintil it reaches 200mmHg. This 200mmHg has to correspond with count 510 of the ADC.

I did some ADC codes but it does not seem to work. I could not figure out the problem. The program will skip the codes in BOLD. It will continue at "BP is taken at...".

I have attached the ADC codes.

Thanks for any help. It's greatly appreciated(:

ADC Program:
c Syntax (Toggle Plain Text)
  1. #include<p18f2620.h>
  2. #include<delays.h>
  3. #include<string.h>
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<math.h>
  7.  
  8. void Init_LCD(void);
  9. void WrLCD(char, char);
  10. void Clear_LCD(void);
  11. void CurPosLCD(char);
  12. void Out_LCD(char rom near*);
  13. void Out_Dec_LCD(unsigned int i);
  14. void Out_Hex_LCD(unsigned int i);
  15. void Puts_LCD(char *);
  16. char Hex2ascii(char);
  17. void LCD_Intro(void);
  18. void disp_val(void);
  19. void adc_pressure(void);
  20. void Delay_msec(unsigned int);
  21. void Delay_sec(unsigned int);
  22.  
  23. #define LCD_DATA PORTB
  24. #define LCD_E PORTBbits.RB1
  25. #define LCD_RS PORTBbits.RB0c
  26.  
  27. #define PB1 PORTAbits.RA4
  28.  
  29. #define MOTOR PORTBbits.RB3
  30. #define VALVE PORTCbits.RC2
  31.  
  32. #define ADC_PRESSURE 0b00000001 // ADC channel 0 for pressure sensor
  33.  
  34. #define ROM_TYPE (char rom near*)
  35. #define OSC_FREQ 32000000
  36.  
  37. #define DELAY1MS 8 // 1ms delay = OSC_FREQ(4*1000*1000)
  38. #define DELAY1S 200 // 1s delay = OSC_FREQ/(4*10000*4)
  39.  
  40. char temp;
  41. unsigned int i,x,y;
  42. int BP;
  43. float Voltage_BP;
  44.  
  45. void main()
  46. {
  47. OSCCON = 0b01110000; // Use internal 8MHz x 4 oscillator
  48. OSCTUNE = 0b01000001; // Enable HSPLL mode
  49. ADCON0 = 0b00000001; // Turn on ADC
  50. ADCON1 = 0b00001110; // AN0 as analog input
  51. ADCON2 = 0b10101010; // 12 TAD; FOSC/32; Right justified
  52.  
  53. TRISA = 0b11111111; // RA4<=button
  54. TRISB = 0b00000100; // PORTB=>LCD_DATA, RB0=>LCD_E, RB1=>LCD_RS;
  55. TRISC = 0b11111001; // RC1 & RC2=>MOTOR and VALVE
  56.  
  57. MOTOR=0;
  58. VALVE=0;
  59.  
  60. Init_LCD();
  61. LCD_Intro();
  62.  
  63. while(1)
  64. {
  65. while (1)
  66. {
  67. CurPosLCD(0x11);
  68. Out_LCD(ROM_TYPE "Put on ");
  69. CurPosLCD(0x20);
  70. Out_LCD(ROM_TYPE "cuff and");
  71.  
  72. if (!PB1)
  73. break;
  74. Delay_sec(1);
  75. CurPosLCD(0x11);
  76. Out_LCD(ROM_TYPE "Press ");
  77. CurPosLCD(0x20);
  78. Out_LCD(ROM_TYPE "<Start> ");
  79. if (!PB1)
  80. break;
  81. Delay_sec(1);
  82. CurPosLCD(0x11);
  83. Out_LCD(ROM_TYPE "250708");
  84. CurPosLCD(0x20);
  85. Out_LCD(ROM_TYPE "07:07:07");
  86.  
  87. if (!PB1)
  88. break;
  89.  
  90. Delay_sec(2);
  91.  
  92. }
  93. WrLCD(0b00000001,0);
  94.  
  95. CurPosLCD(0x21);
  96. Out_LCD(ROM_TYPE "Relax");
  97.  
  98. CurPosLCD(0x13);
  99. Out_LCD(ROM_TYPE "5");
  100. Delay_sec(1);
  101.  
  102. CurPosLCD(0x13);
  103. Out_LCD(ROM_TYPE "4");
  104. Delay_sec(1);
  105.  
  106. CurPosLCD(0x13);
  107. Out_LCD(ROM_TYPE "3");
  108. Delay_sec(1);
  109.  
  110. CurPosLCD(0x13);
  111. Out_LCD(ROM_TYPE "2");
  112. Delay_sec(1);
  113.  
  114. CurPosLCD(0x13);
  115. Out_LCD(ROM_TYPE "1");
  116. Delay_sec(1);
  117.  
  118. WrLCD(0b00000001,0);
  119.  
  120. ADCON0 = ADC_PRESSURE; //ADC channel 0 for Pressure sensor
  121. ADCON0bits.GO = 1; //Start A/D conversion
  122. while(ADCON0bits.DONE);
  123. BP = 256*ADRESH + ADRESL; //Get the value abd storein BP
  124. Voltage_BP = BP*(3300/1024); // Convert to mV
  125.  
  126. if(Voltage_BP<510) // Motor will continue to pump air until it reaches 200mmHg
  127. {
  128. MOTOR=1;
  129. VALVE=1;
  130.  
  131. CurPosLCD(0x10);
  132. Out_LCD(ROM_TYPE "Taking ");
  133. CurPosLCD(0x20);
  134. Out_LCD(ROM_TYPE "your BP ");
  135. Delay_msec(500);
  136.  
  137. WrLCD(0b00000001,0);
  138.  
  139. CurPosLCD(0x10);
  140. Out_LCD(ROM_TYPE " ");
  141. CurPosLCD(0x20);
  142. Out_LCD(ROM_TYPE " ");
  143. Delay_msec(500);
  144. }
  145.  
  146. WrLCD(0b00000001,0);
  147.  
  148. CurPosLCD(0x11);
  149. Out_LCD(ROM_TYPE "BP is");
  150. CurPosLCD(0x20);
  151. Out_LCD(ROM_TYPE "taken at");
  152. Delay_sec(2); // Wait for 3 seconds
  153.  
  154. CurPosLCD(0x11);
  155. Out_LCD(ROM_TYPE "250708");
  156. CurPosLCD(0x20);
  157. Out_LCD(ROM_TYPE "07:07:07");
  158. Delay_sec(2);
  159. WrLCD(0b00000001,0);
  160. disp_val(); // Display BP measurements
  161.  
  162. CurPosLCD(0x11);
  163. Out_LCD(ROM_TYPE "Have a");
  164. CurPosLCD(0x20);
  165. Out_LCD(ROM_TYPE "good day");
  166. Delay_sec(2);
  167. WrLCD(0b00000001,0);
  168. }
  169. }
  170.  
  171. // Initialise LCD
  172. void Init_LCD() // LCD display Initialization
  173. {
  174. Delay_msec(15); // 15ms LCD power up delay
  175. WrLCD(0x03,0);
  176.  
  177. Delay_msec(5); // 5ms delay
  178. WrLCD(0x02,0);
  179. WrLCD(0x28,0); // func set 4-bit, 2lines, 5x7
  180. WrLCD(0x0C,0); // display on, cursor off
  181. WrLCD(0x06,0); // entry mode-inc addr,no shift
  182. WrLCD(0x01,0); // clear display & home position
  183. WrLCD(0x80,0); //Display cursor at home position
  184. }
  185. //Write control/data word to LCD 4-bit at a time
  186. void WrLCD(char x, char i)
  187. {
  188. LCD_RS = i; // Logic '0' to select instruction register
  189. temp = x; // store control word
  190. temp &= 0xF0; // select higher nibble of control word
  191. LCD_DATA &= 0x0F;
  192. LCD_DATA |= temp; // store in LCD_DATA
  193. Delay_msec(1); // 1ms delay
  194. LCD_E = 1; // Activate E pulse
  195. Delay_msec(1); // 1ms delay
  196. LCD_E = 0; // De-activate E pulse
  197. Delay_msec(1); // 1ms delay
  198. temp = x; // store control word
  199. temp <<= 4;
  200. temp &= 0xF0; // select the lower nibble
  201. LCD_DATA &= 0x0F;
  202. LCD_DATA |= temp; // store in LCD_DATA
  203. Delay_msec(1); // 1ms delay
  204. LCD_E = 1; // Activate E pulse
  205. Delay_msec(1); // 1ms delay
  206. LCD_E = 0; // De-activate E pulse
  207. Delay_msec(1); // 1ms delay
  208. }
  209. // Clear the LCD display
  210. void Clear_LCD(void)
  211. {
  212. WrLCD(0x01,1); //Clear display command
  213. WrLCD(0x02,1); //Set cursor to home position
  214. Delay_msec(2);
  215. }
  216. // Set current LCD cursor position
  217. // pos = 0x10 is the first character of the first line
  218. // pos = 0x20 is the first character of the second line
  219. void CurPosLCD(char pos) {
  220. if((0xf0 & pos) == 0x20) {
  221. pos = pos &0x0f;
  222. pos = pos |0xc0;
  223. }
  224. else {
  225. pos = pos & 0x0f;
  226. pos = pos | 0x80;
  227. }
  228. WrLCD(pos, 0);
  229. }
  230.  
  231. // Display const message on LCD
  232. void Out_LCD(char rom near *message)
  233. {
  234. while (*message)
  235. {
  236. WrLCD(*message,1);
  237. message++;
  238. }
  239. }
  240.  
  241. // Print decimal number to LCD
  242. void Out_Dec_LCD(unsigned int i)
  243. {
  244. char digit, zero, value;
  245. long denom;
  246. denom = 1000000000;
  247. zero = 1;
  248. if (!value)
  249. WrLCD('0',1);
  250. else
  251. {
  252. while (denom)
  253. {
  254. digit = value/denom;
  255. if (digit)
  256. {
  257. WrLCD(digit + '0',1);
  258. zero = 0;
  259. }
  260. else if (!zero)
  261. WrLCD('0',1);
  262. value -= digit*denom;
  263. denom /= 10;
  264. }
  265. }
  266. }
  267.  
  268. // Print hexadecimal number to LCD
  269. void Out_Hex_LCD(unsigned int i)
  270. {
  271. unsigned char ctmp0, ctmp1, ctmp2, ctmp3;
  272. ctmp0 = 0x000f & i;
  273. i >>= 4;
  274. ctmp1 = 0x000f & i;
  275. i >>= 4;
  276. ctmp2 = 0x000f & i;
  277. i >>= 4;
  278. ctmp3 = 0x000f & i;
  279. WrLCD(Hex2ascii(ctmp3),1);
  280. WrLCD(Hex2ascii(ctmp2),1);
  281. WrLCD(Hex2ascii(ctmp1),1);
  282. WrLCD(Hex2ascii(ctmp0),1);
  283. }
  284.  
  285. // Convert hexadecimal number to ASCII character
  286. char Hex2ascii(char c)
  287. {
  288. c &= 0x0f;
  289. if(c < 0x0a)
  290. return (c + 0x30);
  291. else
  292. return (c + 0x37);
  293. }
  294.  
  295. // Output variable string to LCD
  296. void Puts_LCD(char *message)
  297. {
  298. while (*message)
  299. {
  300. WrLCD(*message,1);
  301. message++;
  302. }
  303. }
  304.  
  305. // Output initialisation messages
  306. void LCD_Intro(void)
  307. {
  308. Clear_LCD;
  309. CurPosLCD(0x10);
  310. Out_LCD(ROM_TYPE "TEMASEK");
  311. CurPosLCD(0x22);
  312. Out_LCD(ROM_TYPE "POLY ");
  313. Delay_sec(2);
  314.  
  315. CurPosLCD(0x10);
  316. Out_LCD(ROM_TYPE "PORTABLE");
  317. CurPosLCD(0x20);
  318. Out_LCD(ROM_TYPE "BP METER");
  319. Delay_sec(2);
  320. WrLCD(0b00000001,0);
  321. }
  322.  
  323. // Display user's bp measurement
  324. void disp_val()
  325. {
  326. for(y=0; y<2; y++)
  327. {
  328. WrLCD(0x82,0);
  329. Out_LCD(ROM_TYPE "SYS ");
  330. WrLCD(0xC0,0);
  331. Out_LCD(ROM_TYPE "mmHg");
  332. Delay_sec(3);
  333.  
  334. }
  335. }
  336. // Delay multiple number of msec
  337. void Delay_msec(unsigned int delayInMSEC)
  338. {
  339. while (delayInMSEC--)
  340. Delay1KTCYx(DELAY1MS);
  341. }
  342.  
  343. // Delay multiple number of sec
  344. void Delay_sec(unsigned int delayInSEC)
  345. {
  346. while (delayInSEC--)
  347. {
  348. Delay10KTCYx(DELAY1S);
  349. Delay10KTCYx(DELAY1S);
  350. Delay10KTCYx(DELAY1S);
  351. Delay10KTCYx(DELAY1S);
  352. }

Last edited by Ancient Dragon; Aug 12th, 2008 at 7:13 AM. Reason: add code tags
musica07 is offline   Reply With Quote