Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.51 KB | None | 0 0
  1. /*
  2. * File: zadatakful.c
  3. * Author: Admin
  4. *
  5. * Created on 25 March 2019, 20:43
  6. */
  7. // CONFIG
  8.  
  9. #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
  10. #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
  11. #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
  12. #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
  13. #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
  14. #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
  15. #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
  16. #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
  17.  
  18. #include <xc.h>
  19. #include<stdio.h>
  20. #include<stdlib.h>
  21. #include<conio.h>
  22. #define _XTAL_FREQ 20000000
  23. #define Baud_rate 9600
  24.  
  25. #define CRVENA RC1
  26. #define ZUTA RC2
  27. #define RS RD2
  28. #define EN RD3
  29. #define D4 RD4
  30. #define D5 RD5
  31. #define D6 RD6
  32. #define D7 RD7
  33.  
  34. void Lcd_Port(char a){
  35. if(a&1)
  36. D4=1;
  37. else
  38. D4=0;
  39. if(a&2)
  40. D5=1;
  41. else
  42. D5=0;
  43. if(a&4)
  44. D6=1;
  45. else
  46. D6=0;
  47. if(a&8)
  48. D7=1;
  49. else
  50. D7=0;
  51.  
  52. }
  53.  
  54. void Lcd_Cmd(char a){
  55. RS=0;
  56. Lcd_Port(a);
  57. EN=1;
  58. __delay_ms(4);
  59. EN=0;
  60. }
  61.  
  62. Lcd_Clear(){
  63. Lcd_Cmd(0);
  64. Lcd_Cmd(1);
  65. }
  66.  
  67.  
  68. void Lcd_Set_Cursor(char a, char b){
  69. char temp, z, y;
  70. if(a==1){
  71. temp=0x80+b-1;
  72. z=temp>>4;
  73. y=temp&0x0F;
  74. Lcd_Cmd(z);
  75. Lcd_Cmd(y);
  76. }
  77. else if(a==2){
  78. temp=0xC0+b-1;
  79. z=temp>>4;
  80. y=temp&0x0F;
  81. Lcd_Cmd(z);
  82. Lcd_Cmd(y);
  83.  
  84. }
  85. }
  86.  
  87. void Lcd_Init(){
  88. Lcd_Port(0x00);
  89. __delay_ms(20);
  90. Lcd_Cmd(0x03);
  91. __delay_ms(5);
  92. Lcd_Cmd(0x03);
  93. __dely_ms(11);
  94. Lcd_Cmd(0x03);
  95. Lcd_Cmd(0x02);
  96. Lcd_Cmd(0x03);
  97. Lcd_Cmd(0x08);
  98. Lcd_Cmd(0x00);
  99. Lcd_Cmd(0x0C);
  100. Lcd_Cmd(0x03);
  101. Lcd_Cmd(0x06);
  102. }
  103.  
  104. Lcd_Write_Char(char a){
  105. char temp, y;
  106. temp=a&0x0F;
  107. y=a&0x0F;
  108. RS=1;
  109. Lcd_Port(y>>4);
  110. EN=1;
  111. __delay_us(40);
  112. EN=0;
  113. Lcd_Port(temp);
  114. EN=1;
  115. __delay_us(40);
  116. EN=0;
  117. }
  118.  
  119. void Lcd_Write_String(char *a){
  120. int i;
  121. for(i=0;a[i]!='\0';i++){
  122. Lcd_Write_Char(a[i]);
  123.  
  124. }
  125.  
  126. }
  127.  
  128.  
  129. void UART_Init(void){
  130. TRISC6=0;
  131. TRISC7=1;
  132.  
  133. SPBRG=((_XTAL_FREQ/16)/Baud_rate)-1;
  134. BRGH=1;
  135.  
  136. SYNC=0;
  137. SPEN=1;
  138.  
  139. TXEN=1;
  140. CREN=1;
  141.  
  142. TX9=0;
  143. RX9=0;
  144.  
  145.  
  146. }
  147.  
  148. char UART_Send_Char(char a){
  149. while(!TXIF);
  150. TXREG=a;
  151.  
  152. }
  153.  
  154. char UART_Get_Char(){
  155. if(OERR){
  156. CREN=0;
  157. CREN=1;
  158. }
  159. while(!RCIF);
  160. return RCREG;
  161.  
  162. }
  163.  
  164.  
  165. void putch(char a){
  166. UART_Send_Char(a);
  167.  
  168. }
  169.  
  170. void ADC_Initialize(){
  171. ADCON0=0b01000001;
  172. ADCON1=0b11000000;
  173. }
  174.  
  175. unsigned int ADC_Read(unsigned char channel){
  176. ADCON0 &=0X11000101;
  177. ADCON0 |=channel<<3;
  178. __delay_ms(2);
  179. GO_nDONE=1;
  180. while(GO_nDONE);
  181. return((ADRESH<<8)+ADRESL);
  182.  
  183. }
  184.  
  185. void ServoRotiraj0(){
  186. unsigned int i;
  187. for(i=0;i<50;i++){
  188. RC0=1;
  189. __delay_us(800);
  190. RC0=0;
  191. __delay_us(19200);
  192. }
  193. }
  194.  
  195. void ServoRotiraj90(){
  196. unsigned int i;
  197. for(i=0;i<50;i++){
  198. RC0=1;
  199. __delay_us(1500);
  200. RC0=0;
  201. __delay_us(18500);
  202. }
  203. }
  204.  
  205. void ServoRotiraj180(){
  206. unsigned int i;
  207. for(int i=0;i<50;i++){
  208. RC0=1;
  209. __delay_us(2200);
  210. RC0=0;
  211. __delay_us(17800);
  212. }
  213. }
  214.  
  215. void main(void) {
  216.  
  217. PORTB=0;
  218. TRISB=0;
  219. TRISC0=0;
  220. TRISC1=0;
  221. TRISC2=0;
  222. PORTD=0;
  223. TRISD=0;
  224. Lcd_Init();
  225. Lcd_Clear();
  226. UART_Init();
  227. float adc;
  228. float volt, temp;
  229. int C1, C2, C3, C4, temp1;
  230. ADC_Initialize();
  231.  
  232. char DISPLEJ[10]={0x3F, 0x06, 0x05B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
  233. char broj=0;
  234. int opcija;
  235. CRVENA=0;
  236. ZUTA=0;
  237.  
  238. do{
  239. printf("****Dobrodosli!****");
  240. printf("\r1. Rotiranje servo motora");
  241. printf("\r2.Brojanje na 7sef displeju");
  242. printf("\r3.Ispis trenutne temperature na LCD-u");
  243. printf("\r**************************************");
  244. printf("\r Molimo vas da izaberete opciju 1, 2 ili 3");
  245. opcija=UART_Get_Char();
  246. UART_Send_Char(opcija);
  247. printf("\r********");
  248.  
  249. switch(opcija){
  250. case '1':
  251. printf("\r Izabrali ste opciju 1");
  252. Lcd_Set_Cursor(1,1);
  253. Lcd_Write_String("Rotiranje servo motora u jednu,");
  254. Lcd_Set_Cursor(2,1);
  255. Lcd_Write_String("a zatim u drugu stranu 3 puta");
  256.  
  257. for(int i=0;i<3;i++){
  258. ServoRotiraj0();
  259. CRVENA=1;
  260. ZUTA=0;
  261. __delay_ms(150);
  262. ServoRotiraj180();
  263. ZUTA=1;
  264. CRVENA=0;
  265. __delay_ms(150);
  266. }
  267. Lcd_Clear();
  268. ZUTA=0;
  269. CRVENA=0;
  270. break;
  271.  
  272. case '2':
  273. printf("\r\r Izabrali ste opciju 2");
  274. Lcd_Clear();
  275. Lcd_Set_Cursor(1,1);
  276. Lcd_Write_String("Brojanje na 7seg displeju");
  277. Lcd_Set_Cursor(2,1);
  278. Lcd_Write_String("0-9, 0-5, 0-2-4-6-8-1-3-5-7-9-0");
  279.  
  280. for(int i=0;i<10;i++){
  281. PORTB=DISPLEJ[i];
  282. __delay_ms(350);
  283. if(i==9){
  284. __delay_ms(5000);
  285. for(int j=0;j<6;j++){
  286. PORTB=DISPLEJ[j];
  287. __delay_ms(350);
  288. if(j==5){
  289. for(int k=0;k<9;k++){
  290. if(k%2==0){
  291. PORTB=DISPLEJ[k];
  292. __delay_ms(350);
  293. if(k==8){
  294. for(int z=0;z<10;z++){
  295. if(z%2!=0){
  296. PORTB=DISPLEJ[z];
  297. __delay_ms(350);
  298. if(z==9)
  299. PORTB=DISPLEJ[0];
  300. }
  301. }
  302. }
  303. }
  304. }
  305. }
  306. }
  307.  
  308. }
  309.  
  310.  
  311. }
  312. Lcd_Clear();
  313. break;
  314.  
  315.  
  316. case '3':
  317. printf("\r\r Izabrali ste opciju 3");
  318. adc=(ADC_Read(4));
  319. volt=adc*4,88281;
  320. temp=volt/10.0;
  321. temp1=temp*100;
  322.  
  323. C1 =(temp1/1000)%10;
  324. C2 =(temp1/100)%10;
  325. C3 =(temp1/10)%10;
  326. C4 =(temp1/1)%10;
  327.  
  328. printf("\r\r Temperatura: ");
  329. printf("%i",C1);
  330. printf("%i",C2);
  331. printf(".");
  332. printf("%i",C3);
  333. printf("%i",C4);
  334. printf("C");
  335. printf("\r\r");
  336. Lcd_Clear();
  337. Lcd_Set_Cursor(1,3);
  338. Lcd_Write_String("Temperatura ");
  339. Lcd_Set_Cursor(2,5);
  340. Lcd_Write_Char(C1+'0');
  341. Lcd_Write_Char(C2+'0');
  342. Lcd_Write_String(".");
  343. Lcd_Write_Char(C3+'0');
  344. Lcd_Write_Char(C4+'0');
  345. Lcd_Write_Char(0xDF);
  346. Lcd_Write_String("C");
  347. __delay_ms(3000);
  348. Lcd_Clear();
  349. break;
  350.  
  351.  
  352.  
  353.  
  354. }
  355.  
  356.  
  357.  
  358. }
  359. while(opcija!=0);
  360.  
  361.  
  362.  
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement