Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 KB | None | 0 0
  1. /*
  2. * Wyświetlacz.c
  3. *
  4. * Created: 2019-05-08 13:56:16
  5. * Author : oem1
  6. */
  7. //czestotliwosc zegara glownego
  8.  
  9.  
  10. #define F_CPU 16000000
  11.  
  12.  
  13. #include <avr/interrupt.h>
  14.  
  15. #include <avr/io.h>
  16. #include <util/delay.h>
  17. #define DEFSEQ_ConfigPortAsOutput(PortName,IniVal) DDR ## PortName=0xff;\
  18. PORT ## PortName=IniVal;
  19. #define DEFSEQ_ConfigPinAsOutput(PortName,PinNo) DDR ## PortName|=(1<<PinNo);
  20.  
  21. #define DEFSIG_LCD_E_Low PORTA&=~(1<<PA0)
  22. #define DEFSIG_LCD_E_High PORTA|=(1<<PA0)
  23. #define DEFSIG_LCD_RS_Comand PORTA&=~(1<<PA1)
  24. #define DEFSIG_LCD_Rs_Data PORTA|=(1<<PA1)
  25.  
  26. #define DEFCMD_LCD_FunctionSet (1<<5)
  27. #define DEFSET_LCD_DataLenght8 (1<<4)
  28. #define DEFSET_LCD_2Lines (1<<3)
  29. #define DEFSET_LCD_FontSize5x8 (0<<2)
  30.  
  31. #define DEFCMD_LCD_DisplayControl (1<<3)
  32. #define DEFSET_LCD_DisplayOn (1<<2)
  33. #define DEFSET_LCD_CursorOff (0<<1)
  34. #define DEFSET_LCD_CursorOn (1<<1)
  35. #define DEFSET_LCD_BlinkingOff (0<<0)
  36. #define DEFSET_LCD_BlinkingOn (1<<0)
  37.  
  38. #define DEFCMD_LCD_ClearDisplay (1<<0)
  39.  
  40. #define DEFCMD_LCD_ReturnHome (1<<1)
  41.  
  42. #define DEFCMD_LCD_EntryMode (1<<2)
  43. #define DEFSET_LCD_CursorMoveDirInc (1<<1)
  44. #define DEFSET_LCD_CursorMoveDirDec (0<<1)
  45.  
  46. #define DEFCMD_LCD_SetDDRAMAddress (1<<7)
  47.  
  48. #define DEFCMD_LCD_CurDispShift (1<<4)
  49. #define DEFSET_LCD_DisplayShift (1<<3)
  50. #define DEFSET_LCD_CursorShift (0<<3)
  51.  
  52. #define DEFSET_LCD_ShiftLeft (0<<2)
  53. #define DEFSET_LCD_ShiftRight (1<<2)
  54.  
  55. #define DEFCMD_LCDSetCGRAMAddress (1<<6)
  56.  
  57.  
  58.  
  59.  
  60. void Fun_LCD_write(uint8_t ParByte) {
  61. PORTB = ParByte;
  62. DEFSIG_LCD_E_High;
  63. _delay_ms(10);
  64. DEFSIG_LCD_E_Low;
  65.  
  66. }
  67.  
  68. void Fun_ShiftDisplayWindow (uint8_t Par_Dirction) {
  69. DEFSIG_LCD_RS_Comand;
  70. Fun_LCD_write(DEFCMD_LCD_CurDispShift|DEFSET_LCD_DisplayShift|Par_Dirction);
  71. _delay_us(40);
  72. }
  73.  
  74. uint8_t sec=00;
  75.  
  76. uint8_t
  77. VAR_String1[]="Druzyna1: 00",
  78. VAR_String2[]="Druzyna2: 00"
  79.  
  80. ;
  81.  
  82. volatile uint8_t number1[] = {0};
  83. volatile uint8_t number2[] = {0};
  84. volatile uint8_t number3[] = {0};
  85. volatile uint8_t number4[] = {0};
  86.  
  87.  
  88.  
  89. #define DEFVAL_CharSi 0
  90. const uint8_t
  91. Const_CharSi[8]={ 0b00000000,
  92. 0b00000100,
  93. 0b00011111,
  94. 0b00000010,
  95. 0b00000100,
  96. 0b00001000,
  97. 0b00011111,
  98. 0b00000000};
  99.  
  100. void Fun_LCD_Init (void) {
  101. // interface configuration
  102. DEFSEQ_ConfigPortAsOutput (B, 0);
  103. DEFSEQ_ConfigPinAsOutput (A,0)
  104. DEFSEQ_ConfigPinAsOutput (A,1)
  105. DEFSIG_LCD_E_Low;
  106.  
  107. //reset sequence
  108. DEFSIG_LCD_RS_Comand;
  109. _delay_ms(41);
  110. Fun_LCD_write (0b00110000);
  111. _delay_ms(5);
  112. Fun_LCD_write (0b00110000);
  113. _delay_us(110);
  114. Fun_LCD_write (0b00110000);
  115. _delay_us(110);
  116.  
  117. //display configuration
  118. DEFSIG_LCD_RS_Comand;
  119. Fun_LCD_write (DEFCMD_LCD_FunctionSet
  120. |DEFSET_LCD_DataLenght8
  121. |DEFSET_LCD_2Lines
  122. |DEFSET_LCD_FontSize5x8);
  123. _delay_us(40);
  124. Fun_LCD_write (DEFCMD_LCD_DisplayControl
  125. |DEFSET_LCD_DisplayOn
  126. |DEFSET_LCD_CursorOff
  127. |DEFSET_LCD_BlinkingOff);
  128.  
  129. _delay_us(40);
  130. Fun_LCD_write(DEFCMD_LCD_ClearDisplay);
  131.  
  132. _delay_us(40);
  133. Fun_LCD_write(DEFCMD_LCD_ReturnHome);
  134.  
  135. _delay_ms(2);
  136. Fun_LCD_write(DEFCMD_LCD_EntryMode
  137. |DEFSET_LCD_CursorMoveDirInc);
  138.  
  139. _delay_us(40);
  140. Fun_LCD_write(DEFCMD_LCD_SetDDRAMAddress|0x00);
  141.  
  142. _delay_us(40);
  143. }
  144.  
  145. void Fun_DisplayStringAtPossition (uint8_t* Par_Ptr, uint8_t Par_ByteCount, int8_t Par_Line, int8_t Par_Position) {
  146.  
  147.  
  148. //setting the position
  149. if((Par_Line !=-1)&&(Par_Position != -1)) {
  150. DEFSIG_LCD_RS_Comand;
  151. Fun_LCD_write(DEFCMD_LCD_SetDDRAMAddress|((Par_Line==1?0x00:0x40)+Par_Position));
  152. _delay_us(40);
  153. }
  154.  
  155. //contents sending
  156.  
  157. DEFSIG_LCD_Rs_Data;
  158.  
  159. while (Par_ByteCount --) {
  160. Fun_LCD_write(*Par_Ptr ++);
  161.  
  162. _delay_us(40);
  163. }
  164.  
  165. //Fun_LCD_write(*Par_Ptr);
  166.  
  167. }
  168.  
  169.  
  170. void Fun_WriteCharFontToCGRAM (const uint8_t* ParPatern, uint8_t ParAddress){
  171. DEFSIG_LCD_RS_Comand;
  172. Fun_LCD_write(DEFCMD_LCDSetCGRAMAddress | (ParAddress<<3));
  173. _delay_us(40);
  174.  
  175. DEFSIG_LCD_Rs_Data;
  176. uint8_t
  177. LocVAR_Index;
  178. for (LocVAR_Index=0;LocVAR_Index<8;LocVAR_Index ++ )
  179. Fun_LCD_write(ParPatern[LocVAR_Index]);
  180. _delay_us(40);
  181. }
  182.  
  183.  
  184. /////
  185.  
  186. volatile uint8_t key_state=0;
  187. volatile uint8_t key_state2=0;
  188. volatile uint8_t key_state3=0;
  189. volatile uint8_t key_state4=0;
  190.  
  191. ISR(TIMER0_COMP_vect)
  192. {
  193. //static uint8_t cnt=0;
  194. //PORTB |=0X0f;
  195. //PORTA = LED[cnt];
  196. //PORTB &= ~(1<< cnt);
  197. //if (++cnt>3) cnt=0;
  198.  
  199.  
  200. //obsluga przycisku
  201. switch(key_state)
  202. {
  203. case 0: if((PINC & 0b10000000)==0) key_state=1;
  204. break;
  205.  
  206. case 1: if((PINC & 0b10000000)==0) key_state=2;
  207. else key_state=0;
  208. break;
  209.  
  210. case 2:break;
  211.  
  212. case 3: if((PINC & 0b10000000)!=0) key_state=4;
  213. break;
  214.  
  215. case 4: if((PINC & 0b10000000)!=0) key_state=0;
  216. else key_state=3;
  217. break;
  218.  
  219. default: key_state=0;
  220. }
  221.  
  222. switch(key_state2)
  223. {
  224. case 0: if((PINC & 0b01000000)==0) key_state2=1;
  225. break;
  226.  
  227. case 1: if((PINC & 0b01000000)==0) key_state2=2;
  228. else key_state2=0;
  229. break;
  230.  
  231. case 2:break;
  232.  
  233. case 3: if((PINC & 0b01000000)!=0) key_state2=4;
  234. break;
  235.  
  236. case 4: if((PINC & 0b01000000)!=0) key_state2=0;
  237. else key_state2=3;
  238. break;
  239.  
  240. default: key_state2=0;
  241. }
  242. switch(key_state3)
  243. {
  244. case 0: if((PINC & 0b00100000)==0) key_state3=1;
  245. break;
  246.  
  247. case 1: if((PINC & 0b00100000)==0) key_state3=2;
  248. else key_state3=0;
  249. break;
  250.  
  251. case 2:break;
  252.  
  253. case 3: if((PINC & 0b00100000)!=0) key_state3=4;
  254. break;
  255.  
  256. case 4: if((PINC & 0b00100000)!=0) key_state3=0;
  257. else key_state3=3;
  258. break;
  259.  
  260. default: key_state3=0;
  261. }
  262. switch(key_state4)
  263. {
  264. case 0: if((PINC & 0b00010000)==0) key_state4=1;
  265. break;
  266.  
  267. case 1: if((PINC & 0b00010000)==0) key_state4=2;
  268. else key_state4=0;
  269. break;
  270.  
  271. case 2:break;
  272.  
  273. case 3: if((PINC & 0b00010000)!=0) key_state4=4;
  274. break;
  275.  
  276. case 4: if((PINC & 0b00010000)!=0) key_state4=0;
  277. else key_state4=3;
  278. break;
  279.  
  280. default: key_state4=0;
  281. }
  282. }
  283. ////
  284.  
  285.  
  286.  
  287.  
  288.  
  289. int main(void)
  290. {
  291. int sec =50;
  292. Fun_LCD_Init();
  293.  
  294.  
  295. Fun_WriteCharFontToCGRAM (Const_CharSi, DEFVAL_CharSi);
  296. VAR_String2[3]=DEFVAL_CharSi;
  297. VAR_String1[3]=DEFVAL_CharSi;
  298. Fun_DisplayStringAtPossition (VAR_String1, 9, 1, 0);
  299. DEFSIG_LCD_Rs_Data;
  300.  
  301. uint8_t tmp = number1[0]+'0';
  302. Fun_LCD_write(tmp);
  303. _delay_us(40);
  304. uint8_t tmp1 = number2[0]+'0';
  305. Fun_LCD_write(tmp1);
  306. _delay_us(40);
  307.  
  308. Fun_DisplayStringAtPossition (VAR_String2, 9, 2, 0);
  309. uint8_t tmp2 = number3[0]+'0';
  310. Fun_LCD_write(tmp2);
  311. _delay_us(40);
  312. uint8_t tmp3 = number4[0]+'0';
  313. Fun_LCD_write(tmp3);
  314. _delay_us(40);
  315.  
  316.  
  317.  
  318.  
  319.  
  320. /* while (1) {
  321. uint8_t
  322. LocAR_ShiftIndex;
  323. for (LocAR_ShiftIndex=0 ; LocAR_ShiftIndex <10; LocAR_ShiftIndex++) {
  324. Fun_ShiftDisplayWindow (DEFSET_LCD_ShiftLeft);
  325. _delay_ms(1000);
  326. }
  327.  
  328. for (LocAR_ShiftIndex=0 ; LocAR_ShiftIndex <10; LocAR_ShiftIndex++) {
  329. Fun_ShiftDisplayWindow (DEFSET_LCD_ShiftRight);
  330. _delay_ms(1000);
  331. }
  332.  
  333. };
  334.  
  335.  
  336. */
  337.  
  338.  
  339. TCCR0 |= (1<<WGM01);
  340. TCCR0 |= (1<<CS02) |(1<<CS00);
  341. OCR0=50;
  342. //przerwania
  343. TIMSK |= (1<<OCIE0);
  344. sei();
  345.  
  346. ///////PRZYCISKI
  347. PORTC|=0b10000000;
  348. PORTC|=0b01000000;
  349. PORTC|=0b00100000;
  350. PORTC|=0b00010000;
  351.  
  352. while(1){
  353.  
  354. if(key_state==2)
  355. {
  356.  
  357. VAR_String1[11]++;
  358. if (VAR_String1[11]=='9')
  359. {
  360. VAR_String1[10]++;
  361. VAR_String1[11]=='0';
  362. }
  363. Fun_LCD_Init ();
  364. Fun_DisplayStringAtPossition (VAR_String1, 12, 1,0);
  365. Fun_DisplayStringAtPossition (VAR_String2, 12, 2, 0);
  366. key_state=3;
  367. _delay_ms(2000);
  368.  
  369. }
  370. if(key_state2==2)
  371.  
  372. {
  373.  
  374. VAR_String2[11]++;
  375. Fun_LCD_Init ();
  376. Fun_DisplayStringAtPossition (VAR_String1, 12, 1,0);
  377. Fun_DisplayStringAtPossition (VAR_String2, 12, 2, 0);
  378. key_state2=3;
  379. _delay_ms(2000);
  380.  
  381. }
  382.  
  383. if(key_state3==2)
  384. {
  385.  
  386. VAR_String1[11]--;
  387. Fun_LCD_Init ();
  388. Fun_DisplayStringAtPossition (VAR_String1, 12, 1,0);
  389. Fun_DisplayStringAtPossition (VAR_String2, 12, 2, 0);
  390. key_state3=3;
  391. _delay_ms(2000);
  392.  
  393. }
  394. if(key_state4==2)
  395.  
  396. {
  397.  
  398. VAR_String2[11]--;
  399. Fun_LCD_Init ();
  400. Fun_DisplayStringAtPossition (VAR_String1, 12, 1,0);
  401. Fun_DisplayStringAtPossition (VAR_String2, 12, 2, 0);
  402. key_state4=3;
  403. _delay_ms(2000);
  404.  
  405. }
  406. }
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement