Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. /*
  2. * File: lab4.c
  3. * Author: Aaron
  4. *
  5. * Created on February 28, 2017, 9:06 AM
  6. */
  7.  
  8. #include "xc.h"
  9. _CONFIG1 (FWDTEN_OFF & JTAGEN_OFF)
  10. _CONFIG2 (POSCMOD_NONE & OSCIOFNC_ON & FCKSM_CSDCMD & FNOSC_FRCPLL )
  11.  
  12.  
  13. int adcvalue=0;
  14. int value=0;
  15. int t2=0;
  16. void ADC_init(){
  17. //setup ADC configuration bits and TRISB
  18.  
  19. AD1PCFG=0xFDFF;
  20. AD1CON1=0x20E6;
  21. AD1CON2=0;
  22. AD1CON3=0x0201;
  23. AD1CHS=0x0009;
  24. AD1CSSL=0x0000;
  25. TRISB=0x8000;
  26.  
  27. }
  28.  
  29. void setup(void){
  30. __builtin_write_OSCCONL(OSCCON & (~(1<<6))); // Release IOLOCK
  31. //RPOR3bits.RP7R = 0x12; //RB7
  32. RPOR2bits.RP4R = 0x12; //RB4
  33. __builtin_write_OSCCONL(OSCCON|(1<<6)); // Engage IOLOCK
  34. }
  35. void main(void) { //10khz, 75% duty cycle
  36.  
  37. ADC_init(); //initialize ADC
  38.  
  39. //enable interrupt
  40. //clear interrupt flag
  41. IEC0bits.AD1IE = 1;
  42. IFS0bits.AD1IF = 0;
  43. AD1CON1bits.ADON = 1; //turn on ADC
  44.  
  45. //init PWM
  46. OC1CON = 0; //Clear OC1CON1
  47. OC1R=800;
  48. OC1RS=600;
  49. //OC1R = 79;
  50. //OC1RS = 39;
  51. //OC1CON2 = 0x020C; //Set value of OC1CON2
  52. OC1CON = 0x2000;
  53. OC1CONbits.OCM = 0x0006;
  54. //PR2=79;
  55. PR2=800;
  56. T2CONbits.TON = 0b1; //Initiate Timer 2
  57.  
  58. setup();
  59.  
  60. while(1){
  61. }
  62. }
  63. void __attribute__ ((interrupt, no_auto_psv)) _ADC1Interrupt(void){
  64. //Disable interrupt
  65. //Clear flag
  66.  
  67. IEC0bits.AD1IE = 0;
  68. IFS0bits.AD1IF = 0;
  69. //adcvalue = ADC1BUF0;
  70.  
  71. // OC1R=adcvalue*78/1023;
  72. //LATB=adcvalue;
  73.  
  74. IEC0bits.AD1IE = 1;
  75. IFS0bits.AD1IF = 0;
  76.  
  77. }
  78. void __attribute__ ((interrupt,no_auto_psv)) _T2Interrupt(void){
  79. IEC0bits.T2IE=0;
  80. IFS0bits.T2IF=0;
  81. t2=2;
  82.  
  83.  
  84. IEC0bits.T2IE=1;
  85. IFS0bits.T2IF=0;
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement