Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. void adcSetup()
  2. {
  3. OpenADC (
  4. ADC_FOSC_8 &
  5. ADC_RIGHT_JUST &
  6. ADC_20_TAD,
  7. ADC_CH4 &
  8. ADC_INT_OFF &
  9. ADC_REF_VDD_VDD &
  10. ADC_REF_VDD_VSS,
  11. ADC_CH4
  12. );
  13. ENABLE_AN4_ANA();
  14. SetChanADC(ADC_CH4);
  15. }
  16.  
  17. //TODO ADC with interrupts
  18.  
  19. unsigned int adcGet(void)
  20. {
  21. ConvertADC();
  22. while(BusyADC()){}
  23. return ReadADC();
  24. }
  25.  
  26. #include <xc.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <stdbool.h>
  30. #include <p18f66k22.h>
  31. #include <delays.h>
  32. #include <usart.h>
  33.  
  34. #include "defines.h"
  35. #include "configuration-bits.h"
  36. #include "xport.h"
  37. #include "adc.h"
  38.  
  39. #define __delay_us(x) _delay((unsigned long)((x)*(8000000/4000000UL)))
  40. #define __delay_ms(x) _delay((unsigned long)((x)*(8000000/4000UL)))
  41.  
  42. volatile char RxBuf[] = " ";
  43. volatile int RxI = 0;
  44. volatile bool RxMessage = false;
  45. volatile bool ButtonPressed = false;
  46.  
  47. void SetupRegisters(void);
  48. void SetupInterrupts(void);
  49. void interrupt HighISR(void);
  50. void interrupt low_priority LowISR(void);
  51. void delay_ms(unsigned int x);
  52. void buttonHandle(void);
  53. void messageHandle(void);
  54.  
  55. void SetupInterrupts(void)
  56. {
  57. INTCONbits.GIE = 1; //Global interrupt enable
  58. RCONbits.IPEN = 1; //Enable priority interrupts
  59. INTCONbits.GIEH = 1; //Global interrupt enable High
  60. INTCONbits.GIEL = 1; //Global interrupt enable Low
  61. INTCONbits.PEIE = 1; //Peripheral Interrupt Enable bit
  62. INTCONbits.PEIE_GIEL = 1; //Peripheral Interrupt Enable?
  63.  
  64. INTCON3 = 0b00000000; //Clear intcon. INT1 and INT2 are now low priority
  65. INTCON3bits.INT1E = 1; //Enable int1 (BUTTON)
  66. }
  67.  
  68. void SetupRegisters(void)
  69. {
  70. //76543210
  71. TRISA = 0b10101110; //7:RFID en2 6:x 5:POWER_LEVEL(analog) 4:PWRKEY 3:SW_CHRG 2:SW_FAULT 1:EXT_INP
  72. TRISBbits.TRISB1 = 1; //BUTTON INPUT
  73. TRISEbits.TRISE3 = 0; //XPort RESET
  74. TRISGbits.TRISG3 = 1; //LDO pwrgd (input)
  75. TRISGbits.TRISG4 = 0; //LDO shdn (ldo to toggle xport)
  76. }
  77.  
  78. /* Main */
  79. int main() {
  80. //---Set up Registers/interrupts of PIC---
  81. //See defines.h for al macros for LED_IN and other pin-renames.
  82. SetupRegisters(); //Registers...
  83. SetupInterrupts(); //Interrupts (button/uart)
  84. adcSetup(); //ADC for power-detection (POWER-LEVEL)
  85.  
  86. //---Set up peripherals---
  87. xportSetup(); //Using xport as debugging help.
  88. xportEnable(); //Switch ldo to enable it.
  89.  
  90. while(true){
  91. if(RxMessage){
  92. messageHandle();
  93. }else if(ButtonPressed){
  94. buttonHandle();
  95. ButtonPressed = false;
  96. }
  97. }
  98. return 0;
  99. }
  100.  
  101.  
  102. void interrupt high_priority HighIsr(void) //High priority interrupt
  103. {
  104. if(PIR3bits.RC2IF){//USART INTERRUPT
  105. RxBuf[RxI] = RCREG2;
  106. if(RxBuf[RxI] == ';'){//TODO or full
  107. RxMessage = true;
  108. }
  109. RxI++;
  110. }else{
  111. xportSendText("High - unhandled interrupt");
  112. }
  113. }
  114.  
  115. void interrupt low_priority LowIsr(void) //Low priority interrupt
  116. {
  117. if(INT1IF){ //Button interrupt
  118. ButtonPressed = true; //Set flag (handled in main)
  119. INT1IF = false; //clear interrupt flag afterwards to avoid hardware bounce re-interrupt
  120. }else{ //Warning for unhandled interrupt
  121. xportSendText("[ERROR] Low - unhandled interrupt!");
  122. }
  123. }
  124.  
  125. void delay_ms(unsigned int xc)
  126. {
  127. do
  128. {
  129. xc--;
  130. __delay_ms(1);
  131. }
  132. while(xc > 0);
  133. }
  134.  
  135. void buttonHandle(void){
  136. delay_ms(100);
  137. if(!BUTTON){
  138. xportSendText("Button pressed");
  139.  
  140. //ADC DEBUG
  141. char buffer[] = " ";
  142. sprintf (buffer, "ADC: %u", adcGet());
  143. xportSendText(buffer);
  144.  
  145. //RX DEBUG
  146. xportSendText("RxBuf: ");
  147. xportSendText(RxBuf);
  148.  
  149. //XPORT debug
  150. xportDebug();
  151. }
  152. }
  153.  
  154. void messageHandle(void){
  155. xportSendText(RxBuf); //Send/Handle the buffer
  156. strcpy(RxBuf, " "); //Empty the buffer!
  157. RxI = 0; //Start buffer at pos 0 again;
  158. RxMessage = false; //Reset the flag
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement