PIC18F4550 Help on a Homework plz?

Discussion in 'Homework Help' started by Jouy Durão, Dec 6, 2015.

  1. Jouy Durão

    Thread Starter New Member

    Dec 6, 2015
    7
    0
    hardware:
    -Motor DC
    -Bridge H
    -LM35 Temperature Sensor
    -Potenciometro 50k Ohms
    -LCD
    -LEDs

    Software:
    -The LCD should start showing the student's name on the first line and the course name in the second line.
    -If The push button key (CH1) connected to the INT0 interrupt pin is activated, it will allow the A / D conversion perform the pot,
    and this converted value, to the motor PWM cycle;
    -The Key CH2 and CH3 select the motor direction;
    -For Both cases, the LCD shows:
    "Motor powered" - First line of the display if the CH1 is triggered
    "Duty PWM = xxx%" - Second line of the display if the CH2 switch is activated;
    - "Temperature Sensor" - first line of the LCD, if CH1 is not pressed;
    - "Temperature = XXºC" -Second line, CH1 is not pressed through the LED1 indicates the motor direction of rotation;

    So, i'm coding my Pic 18F4550 in MPLAB, and i'm stuck, really, This is my code:

    Code (C):
    1.  
    2. #include<p18f4550.h>
    3. #include<delays.h>
    4. #include<pwm.h>
    5.  
    6. #pragma config CCP2MX=ON  //PINO RC1 - ON
    7. void TOPO (void);
    8. void BOTO (void);
    9. void nome(void);
    10. void configuralcd(void);
    11. void temperatura (void);
    12. void atraso (unsigned char tempo);
    13. void escreve_LETRA(unsigned char letra);
    14. void escreve_CMD(unsigned char caracter);
    15. void string (const rom char *i);
    16.  
    17. #define MAX 40
    18. #define MAX1 500
    19.  
    20. #pragma code ALTA = 0x08  //Interrupção de ALTA prioridade
    21. void ALTA (void)
    22. {
    23.     TOPO();
    24. }
    25. #pragma code
    26. #pragma code BAIXA = 0x18 //Interrupção de BAIXA prioridade
    27. void BAIXA (void)
    28. {
    29.     BOTO();
    30. }
    31. #pragma code
    32.  
    33. int duty_pwm=0;
    34. int a=0;
    35. char graus;
    36. float   X;
    37.  
    38. void main()
    39. {
    40.     char dezena,unidade;
    41.     ADCON0bits.ADON=0; //Desliga o Conversor A/D
    42.     ADCON1= 0b00001111; //Todos os canais com Entrada Digital
    43.     RCONbits.IPEN=0; //Prioridade de Interrupção Desligada
    44.     INTCONbits.GIE=1; //Interrupções Globais Habilitadas
    45.     INTCONbits.PEIE=1; //Interrupções de Periféricos Habilitadas
    46.     INTCONbits.INT0IE=1; //Interrupção INT0 Habilitada
    47.     INTCONbits.INT0IF=0; //Flag de Interrupção ?Limpo?
    48.     INTCON2bits.INTEDG0=1; //Interrupção Sensível a Borda de Subida
    49.     DDRDbits.RD0=0;       //RD0 Saida
    50.     DDRCbits.RC1=0;       //RC1 Saida
    51.  
    52.    //configuração da interrupção
    53.     ADCON1=0x0F;          //Canais analógicos desligados
    54.     RCONbits.IPEN=1;      //INT com prioridade = 1
    55.     INTCONbits.GIE=1;     //INT Globlal = 1
    56.     INTCONbits.PEIE=1;    //INT periferica = 1
    57.     INTCON2bits.INTEDG1=0;//INT 1 em Borda de DESCIDA
    58.     INTCON2bits.INTEDG2=1;//INT 2 em Borda de SUBIDA
    59.     INTCON3=0b01011000;
    60.  
    61.     //configuração do timer
    62.     T2CONbits.T2CKPS0=0;  //    Pre-escala (PS)
    63.     T2CONbits.T2CKPS1=1;  //vvvvvvvvv1:16vvvvvvvvv
    64.     T2CONbits.TMR2ON=1;   //timer 2 = ON
    65.     T2CONbits.T2OUTPS0=0; //------------------------
    66.     T2CONbits.T2OUTPS1=0; //  seleciona pós escala
    67.     T2CONbits.T2OUTPS2=1; //         1:16
    68.     T2CONbits.T2OUTPS3=1; //------------------------
    69.     TMR2=0x00;            //Timer inicia em 0
    70.     PR2=38;               //conta ate 38 e compara com o TRM2, pra gerar um
    71.                           //pulso para a postcale
    72.     //configura o PWM
    73.     CCP2CONbits.CCP2M0=0; //-----------------
    74.     CCP2CONbits.CCP2M1=0; //   Habilita o
    75.     CCP2CONbits.CCP2M2=1; //      PWM2
    76.     CCP2CONbits.CCP2M3=1; //-----------------
    77.  
    78.     configuralcd();
    79.     atraso(MAX);
    80.     nome();
    81.     while(1)
    82.     {
    83.             if(a==1)
    84.     {
    85.         ADCON0bits.GO=1;
    86.         while (ADCON0bits.GO==1);
    87.  
    88.         escreve_CMD(0X01);
    89.  
    90.         dezena=(graus/10)+48;
    91.         PORTEbits.RE0=1;    //dezena
    92.         PORTEbits.RE1=1;
    93.         PORTD=dezena;
    94.         atraso(MAX);
    95.         PORTEbits.RE1=0;
    96.         atraso(MAX);
    97.  
    98.         unidade=(graus%10)+48;
    99.         PORTEbits.RE0=1;   //unidade
    100.         PORTEbits.RE1=1;
    101.         PORTD=unidade;
    102.         atraso(MAX);
    103.         PORTEbits.RE1=0;
    104.         atraso(MAX);
    105.         a=2;
    106.     }
    107.     if(a==2)
    108.     {
    109.         atraso(MAX1);
    110.         atraso(MAX1);
    111.         atraso(MAX1);
    112.         atraso(MAX1);
    113.         nome();
    114.         a=0;
    115.     }
    116.     }
    117. }
    118.  
    119. #pragma interrupt TOP
    120. void TOPO (void)
    121. {
    122.     duty_pwm+=50; //incrementa 50
    123.     if (duty_pwm>156)
    124.     {
    125.         duty_pwm=156;
    126.     }
    127.     else
    128.     {
    129.         SetDCPWM2(duty_pwm);
    130.     }
    131.     INTCON3bits.INT1IF=0; //Int Flag vai pra 0
    132. }
    133. #pragma interrupt BOT
    134. void BOTO (void)
    135. {
    136.     duty_pwm-=50; //decrementa 50
    137.     if (duty_pwm<0)
    138.     {
    139.         duty_pwm=0;
    140.     }
    141.     else
    142.     {
    143.         SetDCPWM2(duty_pwm);
    144.     }
    145.     INTCON3bits.INT2IF=0; //Int Flag vai pra 0
    146. }
    147. void atraso (unsigned char tempo)
    148. {
    149.     int i;
    150.     for(i=0;i<tempo;i++)
    151.     {
    152.         Delay1KTCYx(1);
    153.         Delay1KTCYx(1);
    154.         Delay1KTCYx(1);
    155.         Delay1KTCYx(1);
    156.         Delay1KTCYx(1);
    157.     }
    158. }
    159. void nome(void)
    160. {
    161.     escreve_CMD(0X01);
    162.     string("STUDENT"); //Escreve a primeira linha do DISPLAY
    163.     escreve_CMD(0X00);
    164.     escreve_CMD(0XC0);
    165.     string("COURSE"); //Escreve a segunda linda do DISPLAY
    166. }
    167.  
    168. void configuralcd(void)
    169. {
    170.     escreve_CMD(0x30);//escolha de bits
    171.     escreve_CMD(0x38);//matriz
    172.     escreve_CMD(0x0F);//Cursor Piscante
    173.     escreve_CMD(0x06);//DESLOCAMENTO DO CURSOR
    174. }
    175.  
    176. void escreve_CMD(unsigned char caracter)
    177. {
    178.     PORTEbits.RE0=0;    //COMANDOS
    179.     PORTEbits.RE1=1;
    180.     PORTD=caracter;
    181.     atraso(MAX);
    182.     PORTEbits.RE1=0;
    183.     atraso(MAX);
    184. }
    185.  
    186. void escreve_LETRA(unsigned char letra)
    187. {
    188.     PORTEbits.RE0=1;    //DADOS
    189.     PORTEbits.RE1=1;
    190.     PORTD=letra;
    191.     atraso(MAX);
    192.     PORTEbits.RE1=0;
    193.     atraso(MAX);
    194. }
    195. void string (const rom char *i)
    196. {
    197.     while(*i>0)
    198.     {
    199.         escreve_LETRA(*i++);
    200.     }
    201. }
    202. #pragma interrupt temperatura
    203. void temperatura (void)
    204. {
    205.     int result; //Variável de 16 bits
    206.  
    207.     DDRAbits.RA0 = 1; //Pino A0 como entrada
    208.     ADCON0bits.ADON = 1; //Habilita o conversor A/D
    209.     PIE1bits.ADIE = 0; //Desliga a Interrupção por conversão A/D
    210.     ADCON0bits.CHS0 = 0; //------------------------
    211.     ADCON0bits.CHS1 = 0; // SELECIONA O CANAL
    212.     ADCON0bits.CHS2 = 0; // AN0
    213.     ADCON0bits.CHS3 = 0; //------------------------
    214.     ADCON1 = 0b00001110; //REFERENCIA DE Vss e VDD e o CANAL AN0 como analógico
    215.     ADCON2 = 0b10111101; //FOSC / 16
    216.     //TAD = 20x
    217.     //ADFM = Resultado Justificado á Direita
    218.     ADCON0bits.GO = 1; //Inicializa a Conversão (16 us de aquisição)
    219.     while (ADCON0bits.GO == 1); //Programa Fica parado no WHLE até que o bit GO vá para 0
    220.     result=((unsigned int)ADRESH<<8)|(ADRESL); //RESULTADO EM BINÁRIO
    221.     X=((float)result*5)/1023;
    222.     graus=(X*1)/0.01;
    223.     a=1;
    224.  
    225.     INTCONbits.INT0IF=0; // limpa flag
    226. }
    227.  
    PLZ, can someone JUST HELP ME, PLZ, It's a hurry, I do really need to do this, if someone send me the complete code, do it your way or send me an example so I can understand better and FINISH IT!!
    THX !!!

    and sorry my bad english. ;P

    Mod EDIT: Use code tags to make your code easier to read. Moved this thread to Homework Help.
     
    Last edited by a moderator: Dec 6, 2015
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,022
    Welcome to AAC!

    You will get more help faster if you post a schematic or hookup diagram of your circuit and a detailed description of what is and is not working and what it shows on the display, LEDs etc.
    Which compiler are you using? MPLAB supports several.
    On common problem that I think I see here is that you are not initializing the LCD display correctly. You need delays between the commands (and characters too when you write). If you search on LCD, Hitachi 44780 or similar terms, you'll probably get some hits with code. I've attached a datasheet describing the controller that most dot-matrix LCD modules use. Compare your init code with that.

    Good luck.

    EDIT: A quick search brought up this thread which discusses LCD inits among other things. There are lots of others here.
    Here's a good one with searching help from @nerdegutta (thanks again!)
     
    Last edited: Dec 6, 2015
    nerdegutta likes this.
  3. Jouy Durão

    Thread Starter New Member

    Dec 6, 2015
    7
    0
    I am using compiler C18.

    The LCD is starting correctly, and showing my Name and my Course. But after a Time it should show in the first line "Temperature Sensor" and on the second line it should show the Temperature of LM35, BUT I don't know how to do it...

    And a second question is, how to I control a Motor with a Potentiometer via PIC18F4550, The potentiometer with 50K Ohms need to be connected directly to the Pic, and the Pic needs to read it's OHMs, after reading the resistence, It will make the motor Faster or Slower via a PWM..

    To read the resistence, I need to Enable the A/D? And which is the PWM terminal and A/D Terminal? (I can't find this information and neither remember)
     
  4. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
  5. Jouy Durão

    Thread Starter New Member

    Dec 6, 2015
    7
    0
    Yeah, CCP2, Now I found it hehe, just don't know how to use a Potentiometer to control the PWM and control the High Band (more time in 1 than in 0)
     
  6. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,796
    831
    What does A to D read? Or in other words, what is its input? You have the answers available to you if you don't know.

    Then, what does it output? We know it's digital, but what else can you say about it?
     
  7. Jouy Durão

    Thread Starter New Member

    Dec 6, 2015
    7
    0
    It read the Hardware OHMs from the potentiometer, and in the Output it will make a Motor Faster or Slower.

    And here is a Almost finished circuitry
     
  8. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,796
    831
    You may be confused on what the AD converter is reading. Did you refer to the data sheet?

    Let's pretend the ADC is connected to an MP3 player. What is it reading now? Is it different that the circuit with the potentiometer? Does that make sense if you think it's different?
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    To expand on djsantasi's question... What is the A2D, or the Analog To Digatal converter reading?

    The answer is right in front of you.
     
  10. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,383
    495
    Pot is a voltage divider. What is different is that it is ADJUSTABLE voltage divider. You, the user, rotate the knob and that changes the resistances in the voltage divider, which in turn changes the voltage drop across each resistor that forms the voltage divider. You then feed the voltage from voltage divider to the ANALOG to DIGITAL CONVERTER. Your ADC is reading voltages, not resistances. This same process should be repeated for the temperature sensor.

    As far as temperature, you can use this for an example of the procedure, you will have to write your own code, but the formula should work:
    https://learn.adafruit.com/tmp36-temperature-sensor
    https://learn.adafruit.com/tmp36-temperature-sensor/using-a-temp-sensor
    So the algorithm is:
    1) temp sensor produces voltage, this voltage is sent to ADC
    2) ADC converts the voltage into digital value
    - if ADC is 8 bit, it produces digital values from 0 to 255
    - if ADC is 10 bit, it produces digital values from 0 to 1023
    Read what ADC your PIC has. This is your job. One of many.
    3) the software converts the digital value into temperature using formula
    4) change the temperature from Step 3 into characters
    5) send characters to lcd

    As far as pot. It has three pins, left and right pins will determine the range of voltages, the middle pin is the your knob and when you rotate the knob you move this pin and changes the resistances in the voltage divider. ASSUMING you want values from 0 to +Something, then you connect one side pin to 0 volts/Ground, this is your reference pin/reference voltage of the voltage divider/pot, this is also will be your minimum speed, 0 volts==0 RPM for example. The other side pin is going to be your maximum voltage, this is the voltage that correspond to the maximum speed. Again, I will ASSUME that you are using 5 volt PIC, the maximum voltage that ADC of your PIC can read is 5 volts. So you want to put 5 volts on the other side pin of the pot. To review, you have 0 volts on one side pin of the pot, you have 5 volts on other side pin of the pot. The middle pin of the pot is connected to the ADC of the PIC. The ADC will read the voltage coming out of the voltage divider/pot, convert it into digital value (8 bit: 0-255; 10 bit: 1-1023; etc.) and then your program will look at the digital value and setup the PWM to drive the motor.

    As far as driving the motor. You will have to either setup some kind of Look Up Table or use a formula.
     
  11. Jouy Durão

    Thread Starter New Member

    Dec 6, 2015
    7
    0
    OK, I Did INT0 (interrupt in pin RB0) Work, and now LCD works correctly with strings, commands, and writings.

    But now, I still don't know how to convert, in Analogic, the value in ohms from potentiometer to make the motor run faster or slower.
    I Set RA0 and RA1 (which are two analog pins) to work as analogic, but now, I don't know what to do, how to use it =/

    and don't know how to say a variable on a string to show how much duty is on the LCD screen...
    Like this, for writing on the LCD, I write:
    string("herethetext", 1); (this 1 is the line 1, if 2 then goes to line 2)
    but how do I say the duty via a variable? does it work like
    string("DutyPWM%d%, 2);
    ????
     
  12. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,796
    831
    ADC reads voltage, not ohms. And it does not return the actual voltage. It returns a number from 0 to 1023 (depending on the bit accuracy). So you have to map the numbered returned to what you want your motor to do.
     
  13. Jouy Durão

    Thread Starter New Member

    Dec 6, 2015
    7
    0
    OHHHH, Thanks djsfantasi, now I think I know a way to make it, so, I will need to quantize (or quantizate) (sorry english isn't my main language)
    anyways, there's 3 Prints of the Ready circuit.

    Now i'll try to implement the PWM and LM35 Reader to the code.
    =D
     
  14. Jouy Durão

    Thread Starter New Member

    Dec 6, 2015
    7
    0
    SO, I did the Code, and now I need to write on the LCD the DUTY PWM, but, I don't know how to calculate it right, and get it to work...
    CAN SOMEONE GIVE A LIGHT?
    I don't know how to quantizate it from 100 to 0%, and show it on the display and the PWM output (RC1 on PIC) work correctly.
    Here is my code, feel free to change it =P
    The TMR(void) and INTerruption are the part of PWM
    Code (C):
    1. #include<p18f4550.h>
    2. #include<delays.h>
    3.  
    4. #pragma config CCP2MX=ON  //PINO RC1 - ON (Saida PWM)
    5. #pragma config FOSC=HS // Fosc = 20MHz
    6. #pragma config PWRT=OFF
    7. #pragma config CPUDIV=OSC1_PLL2 //Divisor de Frequência == 1
    8. #pragma config WDT=OFF //Watchdog Desligado
    9. #pragma config MCLRE=ON //Master Clear Ligado
    10. #pragma config LVP=OFF //Low Program Voltage Desligado
    11. #pragma config BOR=OFF //Brown-Out Timer Desligado
    12.  
    13. //void string()
    14. #define RS PORTEbits.RE0
    15. #define RW PORTEbits.RE2
    16. #define ENA PORTEbits.RE1
    17.  
    18. void delay_ms(unsigned int tempo);
    19. void delay_us(unsigned int tempo);
    20. void comandalcd(unsigned char valor, char valor_rs);
    21. void escrevelcd(unsigned char valor);
    22. void configlcd(void);
    23. void string(const rom char *i, unsigned char linha);
    24. void INT(void);
    25. void temperatura(void);
    26. void escrevetemperatura(void);
    27. void TMR(void);
    28.  
    29.  
    30. #pragma code end_INTE = 0x08  //Interrupção de BAIXA prioridade
    31.  
    32. void end_INTE(void)
    33. {
    34.     INT();
    35. }
    36. #pragma code
    37.  
    38. int duty_pwm = 0;
    39. int a = 0;
    40. char graus;
    41. char b = 0;
    42. char c = 0;
    43. float X;
    44. int motor = 0;
    45. int ENG = 0;
    46. char configTMR = 0;
    47.  
    48. void main()
    49. {
    50.     char dezena, unidade;
    51.     DDRA = 0b11000011;
    52.     DDRC = 0x00;
    53.     DDRD = 0x00;
    54.     DDRE = 0b00001000;
    55.     DDRB = 0b01100001;
    56.     PORTB = 0x00;
    57.     PORTD = 0x00;
    58.     PORTA = 0x00;
    59.     PORTC = 0x00;
    60.     PORTE = 0x00;
    61.  
    62.     //configuração da interrupção
    63.     ADCON0bits.ADON = 0x00; //Desliga o Conversor A/D
    64.     ADCON1 = 0b00001111; //Todos canais como entrada Digital
    65.     RCONbits.IPEN = 0; //Prioridade de Interrupção ligada
    66.     INTCONbits.GIEL = 1; //Habilita INT sem Mascara DataSheet
    67.     INTCONbits.GIE = 1; // Interrupções Globais Habilitadas
    68.     INTCONbits.PEIE = 1; //Interrupções de Periféricos Habilitadas
    69.     INTCONbits.INT0IE = 1; //Habilita INT0
    70.     INTCONbits.INT0IF = 0; //Limpra flag INT0
    71.     INTCON2bits.INTEDG0 = 1; //Interrupção Sensível a Borda de Subida
    72.  
    73.     configlcd();
    74.     string("GABRIELA", 1);
    75.     string("ELETRONICA", 2);
    76.     delay_ms(50);
    77.     while (1)
    78.     {
    79.         if (a == 0)
    80.         {
    81.             temperatura();
    82.             if (b == 0) //se a temperatura estiver diferente
    83.             {
    84.                 comandalcd(0x01, 0); //limpa a tela
    85.                 string("TEMPERATURA", 1); //escreve na linha 1
    86.                 delay_us(5);
    87.                 comandalcd(0xC0, 0); //pula linha
    88.                 escrevetemperatura(); //escreve a temperatura
    89.                 escrevelcd('C'); //escreve o C de Celsius após a temperatura
    90.             }
    91.             else if (b == 1) //se a temperatura estiver igual
    92.             {
    93.                 temperatura();
    94.             }
    95.         }
    96.         if (motor >= 1)
    97.         {
    98.             motor = 0;
    99.         }
    100.         if (PORTBbits.RB5 == 1 && PORTBbits.RB6 == 0) //SEN1
    101.         {
    102.             PORTBbits.RB3 = 1; //Saida como SEN1
    103.             PORTBbits.RB4 = 0; //DESLIGA o SEN2
    104.             PORTBbits.RB7 = 1; //LIGA o LED
    105.         }
    106.         else if (PORTBbits.RB5 == 0 && PORTBbits.RB6 == 1) //SEN2
    107.         {
    108.             PORTBbits.RB3 = 0; //DESLIGA o SEN1
    109.             PORTBbits.RB4 = 1; //Saida como SEN2
    110.             PORTBbits.RB7 = 0; //DESLIGA o LED
    111.         }
    112.     }
    113. }
    114.  
    115. #pragma interrupt INT
    116.  
    117. void INT(void)
    118. {
    119.     int result;
    120.     char centena, dezena, unidade;
    121.     if (INTCONbits.INT0IF == 1)
    122.     {
    123.         if (configTMR == 0)
    124.         {
    125.             TMR();
    126.         }
    127.         ADCON1 = 0b00001011; //PORTAS RA0 e RA1 viram analógicas
    128.         comandalcd(0x01, 0); //apaga LCD
    129.         string("MOTOR ACIONADO", 1);
    130.         string("Duty PWM = ", 2);
    131.         ADCON0bits.ADON = 1; //Habilita o conversor A/D
    132.         PIE1bits.ADIE = 0; //Delisga a interrupção por converão A/D
    133.         ADCON0bits.CHS0 = 0; //------------------------
    134.         ADCON0bits.CHS1 = 0; // SELECIONA O CANAL
    135.         ADCON0bits.CHS2 = 0; // AN0
    136.         ADCON0bits.CHS3 = 0; //------------------------
    137.         ADCON1 = 0b00001110; //Referencia de VSS e VDD e o canal AN0 como analóg
    138.         ADCON2 = 0b10111101; //FOSC /16
    139.         ADCON0bits.GO = 1; //inicializa a conversão (leva 16us)
    140.         while (ADCON0bits.GO == 1); //Programa Fica parado no WHLE até que o bit GO vá para 0
    141.         result = ((unsigned int) ADRESH << 8) | (ADRESL); //RESULTADO EM BINÁRIO
    142.         X = ((float) result * 5) / 1023;
    143.         SetDCPWM2(X); //envia o sinal do PWM para o Motor
    144.         ENG = (X * 1) / 0.01;
    145.         centena = (ENG / 100) + 48;
    146.         PORTD = centena;
    147.         PORTE = 0b00000011; // ENABLE e RS em 1
    148.         delay_us(100);
    149.         PORTE = 0b00000001; // ENABLE em 0 RS em 1
    150.         delay_us(100);
    151.         dezena = (ENG / 10) + 48;
    152.         PORTD = dezena;
    153.         PORTE = 0b00000011; // ENABLE e RS em 1
    154.         delay_us(100);
    155.         PORTE = 0b00000001; // ENABLE em 0 RS em 1
    156.         delay_us(100);
    157.         unidade = (ENG % 10) + 48;
    158.         PORTD = unidade;
    159.         PORTE = 0b00000011; // ENABLE e RS em 1
    160.         delay_us(100);
    161.         PORTE = 0b00000001; // ENABLE em 0 RS em 1
    162.         delay_us(100);
    163.         motor = 1;
    164.     }
    165.     //ADCON0bits.ADON = 0;
    166.     /*duty_pwm+=50; //incrementa 50
    167.     if (duty_pwm>156)
    168.     {
    169.         duty_pwm=156;
    170.     }
    171.     else
    172.     {
    173.         SetDCPWM2(duty_pwm);
    174.     }*/
    175.     a = 0;
    176.     INTCONbits.INT0IF = 0; //Int1 Flag vai pra 0
    177. }
    178.  
    179. void TMR(void)
    180. {
    181.     //configuração do timer
    182.     T2CONbits.TMR2ON = 1; //timer 2 = ON
    183.     T2CONbits.T2CKPS0 = 1; //    Pre-escala (PS)
    184.     T2CONbits.T2CKPS1 = 1; //vvvvvvvvv1:16vvvvvvvvv
    185.     TMR2 = 0x00; //Timer inicia em 0
    186.     PR2 = 255; //conta ate 819us e compara com o TRM2, pra gerar um
    187.     //pulso para a postcale
    188.  
    189.     //configura o PWM
    190.     CCP2CONbits.CCP2M0 = 1; //-----------------
    191.     CCP2CONbits.CCP2M1 = 1; //   Habilita o
    192.     CCP2CONbits.CCP2M2 = 1; //      PWM2
    193.     CCP2CONbits.CCP2M3 = 1; //-----------------
    194.     configTMR = 1;
    195. }
    196.  
    197. void temperatura(void)
    198. {
    199.     int result; //Variável de 16 bits
    200.  
    201.     DDRAbits.RA1 = 1; //Pino A1 como entrada
    202.     ADCON0bits.ADON = 1; //Habilita o conversor A/D
    203.     PIE1bits.ADIE = 0; //Desliga a Interrupção por conversão A/D
    204.     ADCON0bits.CHS0 = 1; //------------------------
    205.     ADCON0bits.CHS1 = 0; // SELECIONA O CANAL
    206.     ADCON0bits.CHS2 = 0; // AN0
    207.     ADCON0bits.CHS3 = 0; //------------------------
    208.     ADCON1 = 0b00001101; //REFERENCIA DE Vss e VDD e o CANAL AN0 e AN1 como analógico
    209.     ADCON2 = 0b10111101; //FOSC / 16
    210.     //TAD = 20x
    211.     //ADFM = Resultado Justificado á Direita
    212.     ADCON0bits.GO = 1; //Inicializa a Conversão (16 us de aquisição)
    213.     while (ADCON0bits.GO == 1); //Programa Fica parado no WHILE até que o bit GO vá para 0
    214.     result = ((unsigned int) ADRESH << 8) | (ADRESL); //RESULTADO EM BINÁRIO
    215.     X = ((float) result * 5) / 1023;
    216.     graus = (X * 1) / 0.01;
    217.     if (c == graus)
    218.     {
    219.         b = graus;
    220.     }
    221.     else if (c != graus)
    222.     {
    223.         b = 0;
    224.         c = graus;
    225.         //string("TEMPERATURA", 1);
    226.     }
    227. }
    228.  
    229. void escrevetemperatura(void)
    230. {
    231.     char dezena, unidade;
    232.     temperatura();
    233.     ADCON0bits.GO = 1;
    234.     while (ADCON0bits.GO == 1);
    235.     dezena = (graus / 10) + 48;
    236.     PORTD = dezena;
    237.     PORTE = 0b00000011; // ENABLE e RS em 1
    238.     delay_us(100);
    239.     PORTE = 0b00000001; // ENABLE em 0 RS em 1
    240.     delay_us(100);
    241.     unidade = (graus % 10) + 48;
    242.     PORTD = unidade;
    243.     PORTE = 0b00000011; // ENABLE e RS em 1
    244.     delay_us(100);
    245.     PORTE = 0b00000001; // ENABLE em 0 RS em 1
    246.     PORTE = 0b00000000; //Setando todo o PORTE para Zero
    247. }
    248.  
    249. void comandalcd(unsigned char valor, char valor_rs)
    250. {
    251.     PORTEbits.RE0 = valor_rs; // PINO RS 1=caracter 0=comando
    252.     PORTEbits.RE2 = 0; // RW 0=escrever
    253.     PORTD = valor; //Recebe o caracter ou comando para o PORTD
    254.     PORTEbits.RE1 = 1; //Pino ENABLE ligado
    255.     delay_us(1);
    256.     PORTEbits.RE1 = 0; //Pino ENABLE desligado
    257.     delay_us(400);
    258. }
    259.  
    260. void escrevelcd(unsigned char valor)
    261. {
    262.     PORTD = valor;
    263.     PORTE = 0b00000011; //Pino ENABLE e RS em 1
    264.     delay_us(4);
    265.     PORTE = 0b00000001; //Pino ENABLE em Zero
    266.     delay_us(4);
    267.     PORTE = 0b00000000; //PORTAS RE em Zero
    268. }
    269.  
    270. void configlcd()
    271. {
    272.     comandalcd(0x30, 0); //Ativa modo 8Bits
    273.     comandalcd(0x38, 0); //8Bits com 2 linhas e font de 5x7
    274.     comandalcd(0x01, 0); //Limpa Display
    275.     comandalcd(0x0c, 0); //Liga Display, Cursor Desligado
    276.     comandalcd(0x06, 0); //Incrementa Cursor
    277.     comandalcd(0x0f, 0); //Ativa Cursor Piscante
    278. }
    279.  
    280. void string(const rom char *i, unsigned char linha)
    281. {
    282.     if (linha == 1)
    283.     {
    284.         //comandalcd(0x01, 0); //Curso linha 1 col 1
    285.         while (*i)
    286.         {
    287.             escrevelcd(*i); //Caracter
    288.             i++;
    289.         }
    290.     }
    291.     else if (linha == 2)
    292.     {
    293.         comandalcd(0xC0, 0); //Pula Linha e
    294.         while (*i) //cursor col 1 lin 2
    295.         {
    296.             escrevelcd(*i); //Caracter
    297.             i++;
    298.         }
    299.     }
    300.  
    301. }
    302.  
    303. void delay_ms(unsigned int tempo)
    304. {
    305.     unsigned int i;
    306.     for (i = 0; i < tempo; i++)
    307.     {
    308.         Delay1KTCYx(500);
    309.     }
    310. }
    311.  
    312. void delay_us(unsigned int tempo)
    313. {
    314.     unsigned int i;
    315.     for (i = 0; i < tempo; i++)
    316.     {
    317.         Delay1TCY(); //atrasa 0.2us
    318.         Delay1TCY();
    319.         Delay1TCY();
    320.         Delay1TCY();
    321.         Delay1TCY();
    322.     }
    323. }
     
Loading...