Advertisement
Guest User

2 laba

a guest
May 21st, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.30 KB | None | 0 0
  1.  
  2. // PIC18F4525 Configuration Bit Settings
  3.  
  4. // 'C' source line config statements
  5.  
  6. // CONFIG1H
  7. #pragma config OSC = HSPLL      // Oscillator Selection bits (HS oscillator, PLL enabled (Clock Frequency = 4 x FOSC1))
  8. #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
  9. #pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
  10.  
  11. // CONFIG2L
  12. #pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
  13. #pragma config BOREN = ON       // Brown-out Reset Enable bits (Brown-out Reset enabled and controlled by software (SBOREN is enabled))
  14. #pragma config BORV = 3         // Brown Out Reset Voltage bits (Minimum setting)
  15.  
  16. // CONFIG2H
  17. #pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
  18. #pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
  19.  
  20. // CONFIG3H
  21. #pragma config CCP2MX = PORTC   // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
  22. #pragma config PBADEN = OFF     // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
  23. #pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
  24. #pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
  25.  
  26. // CONFIG4L
  27. #pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
  28. #pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
  29. #pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
  30.  
  31. // CONFIG5L
  32. #pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-003FFFh) not code-protected)
  33. #pragma config CP1 = OFF        // Code Protection bit (Block 1 (004000-007FFFh) not code-protected)
  34. #pragma config CP2 = OFF        // Code Protection bit (Block 2 (008000-00BFFFh) not code-protected)
  35.  
  36. // CONFIG5H
  37. #pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
  38. #pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
  39.  
  40. // CONFIG6L
  41. #pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-003FFFh) not write-protected)
  42. #pragma config WRT1 = OFF       // Write Protection bit (Block 1 (004000-007FFFh) not write-protected)
  43. #pragma config WRT2 = OFF       // Write Protection bit (Block 2 (008000-00BFFFh) not write-protected)
  44.  
  45. // CONFIG6H
  46. #pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
  47. #pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
  48. #pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
  49.  
  50. // CONFIG7L
  51. #pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks)
  52. #pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)
  53. #pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)
  54.  
  55. // CONFIG7H
  56. #pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)
  57.  
  58. #define lcd_clear() lcd_command(1)
  59. #define lcd_origin() lcd_command(2)
  60. #ifndef _XTAL_FREQ
  61. #define _XTAL_FREQ 40000000
  62. #endif
  63. #define E_pulse_with 50 //???????????? ???????? ????????? LCD
  64. #define LCD_E PORTDbits.RD3
  65. #define LCD_RS PORTDbits.RD2
  66. #define LCD_Data4 LATD // ????????????? D4-D7 LCD
  67.  
  68. #include <xc.h>
  69. #include <stdio.h>
  70. #include <stdlib.h>
  71.  
  72. unsigned char num;
  73.  
  74. void strreverse(char* begin, char* end) {
  75.    
  76.     char aux;
  77.     while(end>begin)
  78.         aux=*end, *end--=*begin, *begin++=aux;
  79.    
  80. }
  81.  
  82. void itoa(char* str, int value, int base)
  83. {
  84.     static char num[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  85.     char* wstr=str;
  86.     int sign;
  87.     div_t res;
  88.     // Validate base
  89.     if (base<2 || base>35)
  90.     { *wstr='\0'; return; }
  91.     // Take care of sign
  92.     if ((sign=value) < 0) value = -value;
  93.     // Conversion. Number is reversed.
  94.     do {
  95.         res = div(value,base);
  96.         *wstr++ = num[res.rem];
  97.     }while((value=res.quot));
  98.     if(sign<0) *wstr++='-';
  99.    
  100.     *wstr='\0';
  101.     // Reverse string
  102.     strreverse(str,wstr-1);
  103.    
  104. }
  105.  
  106. void Adc_init()
  107. {
  108. TRISA|=0b00101111; //??????? ??????? RA0, RA1, RA2, RA3, RA5 ? ?????
  109.                     //??????
  110. TRISE|=0b00000111; //??????? ??????? RE0, RE1, RE2 ? ????? ??????
  111. ADCON1bits.PCFG=0b0111; // ???????????? ???????-???????? ??????
  112. ADCON1bits.VCFG=0b00; // ??????? ?????????? Vss Vdd
  113. ADCON2bits.ACQT=0b111;// ????? ?????????????? 20 Tad
  114. ADCON2bits.ADCS=0b110;// ??????? ?????????????? Fosc/64
  115. ADCON2bits.ADFM=0;//????? ????????
  116. ADCON0bits.ADON=1; // ?????? ??? ???????
  117. }
  118.  
  119. int read_Adc(num)
  120. {
  121. ADCON0bits.CHS=num; // ????? ?????? ??????
  122. ADCON0bits.GO_DONE=1; // ???????? ??????????????
  123. while(ADCON0bits.GO_DONE==1);
  124. return (ADRESH<<2)+(ADRESL>>6);//?????? ??????????
  125. }
  126.  
  127.  
  128. void lcd_clk(void) /*????????? ???????? ?? ???? EN*/
  129. {
  130. LCD_E = 1;
  131. __delay_us(E_pulse_with);
  132. LCD_E = 0;
  133. __delay_us(E_pulse_with);
  134. }
  135.  
  136. void lcd_command(unsigned char outbyte) //????????? ??????? (4-?????? ????? ??????)
  137. {
  138. LCD_RS=0; //????? ???????? ???????
  139. LCD_Data4=(LCD_Data4&0x0f)|(outbyte&0xf0); // ???????? ??????? ??????? ???
  140. lcd_clk();
  141. LCD_Data4=(LCD_Data4&0x0f)|((outbyte<<4)&0xf0); // ???????? ??????? ??????? ???
  142. lcd_clk();
  143. __delay_ms(1);
  144. }
  145. void lcd_putc(char outbyte) /* ????????? ?????? (4-?????? ????????) */
  146. {
  147. LCD_RS=1; //????? ???????? ??????
  148. LCD_Data4=(LCD_Data4&0x0f)|(outbyte&0xf0);
  149. lcd_clk();
  150. LCD_Data4=(LCD_Data4&0x0f)|((outbyte<<4)&0xf0);
  151. lcd_clk();
  152. }
  153.  
  154. void lcd_puts(unsigned char line,const char *p) // *????? ?????? ?? ?????*
  155. {
  156. lcd_origin(); // ??????? ? ???????? ?????? LCD
  157. lcd_command(line); // ?????????? ????? LCD 00H
  158. while(*p) // ?????????, ????? ?? ????????? 0
  159. {
  160. lcd_putc(*p); // ????????? ?????? ?? LCD
  161. p++; // ????????? ????? ?? 1
  162. }
  163. }
  164.  
  165. void inttolcd(unsigned char posi, long value) //????? ?? ????? ???????? ??????????
  166. {
  167.     char buff[16];
  168.     itoa(buff,value,10);
  169.     lcd_puts(posi,buff);
  170. }
  171.  
  172. void lcd_init() // ????????????? LCD-???????
  173. {
  174. TRISD &= 0x03;// ??????? ??????? RD4-RD7? ????? ???????
  175. LCD_Data4 &= 0b00001111;//????????? ????? ?? ????? ???????? ??????
  176. LCD_E=0;
  177. LCD_RS=0;
  178. __delay_ms(1);
  179. /*????????????? ???????*/
  180. LCD_Data4=(LCD_Data4&0x0f)|0x30;
  181. lcd_clk();
  182. __delay_ms(1);
  183. LCD_Data4=(LCD_Data4&0x0f)|0x30;
  184. lcd_clk();
  185. __delay_ms(1);
  186. LCD_Data4=(LCD_Data4&0x0f)|0x30;
  187. lcd_clk();
  188. __delay_ms(1);
  189. /*---------------------------------*/
  190. LCD_Data4=(LCD_Data4&0x0f)|0x20; // ??????????? ?? 4-?????? ????? ????????                            
  191. lcd_clk();
  192. __delay_ms(1);
  193. lcd_command(0x28);//?????????? N=1, F=0 (??? ??????, ?????? ??????? 5*8 ?????
  194. lcd_command(0x01); // ???????? ???
  195. lcd_command(0x06); // ????????????? ??????????? ?????? ????? ?????
  196. lcd_command(0x0C); // ??????? ???????, ??????? ???, ?? ???????
  197. lcd_command(0x02); // ????????? ???????
  198. lcd_command(0x01); // ???????? ??? ?????
  199. }
  200.  
  201. unsigned char get_num(int res)
  202. {
  203.        
  204.    
  205.         if(res >=0 && res <= 146)
  206.             num = 0;
  207.         else if(res >=147 && res <= 293)
  208.             num = 1;
  209.         else if(res >=294 && res <= 440)
  210.             num = 2;
  211.         else if(res >=441 && res <= 587)
  212.             num = 3;
  213.         else if(res >=588 && res <= 734)
  214.             num = 4;
  215.         else if(res>=735 && res <= 881)
  216.             num = 5;
  217.         else if(res>=882 && res <= 1023)
  218.             num = 6;
  219.        
  220.         return num;
  221. }
  222.  
  223. int main(int argc, char** argv) {
  224.  
  225.     Adc_init();
  226.     lcd_init();
  227.     while(1)
  228.     {
  229.         get_num(read_Adc(7));
  230.         lcd_clear();
  231.         read_Adc(num);
  232.         inttolcd(0x00, num);
  233.         lcd_puts(0xA8, "Value ");
  234.         inttolcd(0xC9, read_Adc(num)); // ???????? ?????
  235.         __delay_ms(50);
  236.         __delay_ms(50);
  237.        
  238.     }
  239.     return (EXIT_SUCCESS);
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement