Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. #include <16f883.h>
  2. #use fast_io(a)
  3. #use fast_io(b)
  4. #use fast_io(c)
  5. #fuses HS,WDT,PROTECT,NOLVP,NODEBUG,NOMCLR,NOBROWNOUT
  6. #use delay(clock=10000000)
  7. #use RS232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,parity=N,errors,stream=PORTA,RESTART_WDT)
  8.  
  9. /*
  10.  
  11.  
  12. Notas:
  13.  
  14.  
  15. Pino 1 PORTC Como entrada
  16.  
  17. Pino 1,2,3,4,5 PORTB como saída
  18.  
  19.  
  20. Startbyte =#
  21.  
  22. */
  23.  
  24. int16 i=0;
  25. int8 dimming; //0-> 100%, 1-> 75%, 2-> 50%, 3-> 25%
  26.  
  27. //output_a(0x00);
  28.  
  29.  
  30. Unsigned int8 StartByte='#';
  31. Unsigned int8 Data_Buffer[9];
  32. int8 data_receive=0;
  33. int8 current_data=0;
  34. int8 BufferOK=0;
  35. int8 modo=0;
  36.  
  37.  
  38.  
  39.  
  40.  
  41. //Rotina de Interrupção do Pino RX
  42. #INT_RDA
  43.  
  44. void trata_INT_RDA()
  45. {
  46.  
  47. data_receive=fgetc(PORTA); // Recebo o comando da porta Serie
  48.  
  49. if(data_receive==StartByte) //
  50. {
  51. //TimeOut=0;
  52. //count_TimerA=0;
  53. current_data=0;
  54. Data_Buffer[current_data]=data_receive;
  55. current_data++;
  56. BufferOK=0;
  57. }
  58. else
  59. {
  60. if(current_data==8 || data_receive==13) // || Timeout
  61. {
  62. Data_Buffer[current_data]=data_receive;
  63. current_data=0;
  64. bufferOK=1;
  65. if(data_receive!=13) //erro
  66. bufferOK=0;
  67. }
  68. else
  69. {
  70. Data_Buffer[current_data]=data_receive;
  71. current_data++;
  72. }
  73. }
  74.  
  75. }
  76. restart_wdt();
  77.  
  78. // Separar e utilizar a mensagem recebida por RS232
  79. void trata_buffer()
  80. {
  81.  
  82. switch(Data_Buffer[1])
  83.  
  84. {
  85. case 'M':
  86. if(Data_Buffer[2]=='1')
  87. {
  88. modo=0;
  89. //write eeprom
  90. }
  91. if(Data_Buffer[2]=='2')
  92. {
  93. modo=1;
  94. }
  95. if(Data_Buffer[2]=='3'&& Data_Buffer[3]=='\n')
  96. {
  97. modo=2;
  98. }
  99. if(Data_Buffer[2]=='4')
  100. {
  101. dimming=3;
  102. }
  103. break;
  104. /* case 'D':
  105. if(
  106. */
  107. }}
  108. restart_wdt();
  109.  
  110.  
  111. void main()
  112. {
  113.  
  114.  
  115. set_tris_a(0b00110011);
  116. set_tris_c(0b10100000); //Coloca o RC5 como input
  117. set_tris_b(0x00); // Coloca todo o Porto B como Output
  118. setup_adc(ADC_OFF);
  119. enable_interrupts(INT_RDA);
  120. enable_interrupts(global);
  121. //SETUP_TIMER_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_1);
  122.  
  123. Setup_wdt(WDT_2304MS);
  124. restart_wdt();
  125. output_low(PIN_B1); //Start test
  126. output_low(PIN_B2);
  127. output_low(PIN_B3);
  128. output_low(PIN_B4);
  129. output_low(PIN_B5);
  130. restart_wdt();
  131.  
  132.  
  133. while(true)
  134. {
  135.  
  136. restart_wdt();
  137.  
  138. if(bufferOK=1)
  139. {
  140. delay_ms(2);
  141. trata_buffer();
  142. bufferOK=0;
  143. }
  144. restart_wdt();
  145.  
  146. if(input(PIN_C5)) // Caso o relogio tenha ativado modo NOITE
  147. {
  148. Dimming=0;
  149. }
  150. restart_wdt();
  151. if(modo=='0')
  152. {
  153. output_high(PIN_B1); //Start test
  154. output_high(PIN_B2);
  155. output_high(PIN_B3);
  156. output_high(PIN_B4);
  157. output_high(PIN_B5);
  158. delay_ms(1000);
  159. output_low(PIN_B1); //Start test
  160. output_low(PIN_B2);
  161. output_low(PIN_B3);
  162. output_low(PIN_B4);
  163. output_low(PIN_B5);
  164. }
  165.  
  166. if(modo=='1')
  167. {
  168. output_high(PIN_B1); //Start test
  169. output_high(PIN_B2);
  170. output_high(PIN_B3);
  171. output_high(PIN_B4);
  172. output_high(PIN_B5);
  173. output_low(PIN_B1); //Start test
  174. output_low(PIN_B2);
  175. output_low(PIN_B3);
  176. output_low(PIN_B4);
  177. output_low(PIN_B5);
  178. }
  179. restart_wdt();
  180. /*
  181. switch (modo)
  182. {
  183. case '0':
  184. output_high(PIN_B1); //Start test
  185. output_high(PIN_B2);
  186. output_high(PIN_B3);
  187. output_high(PIN_B4);
  188. output_high(PIN_B5);
  189. break;
  190.  
  191. case '1':
  192. output_high(PIN_B1); //Start test
  193. output_high(PIN_B2);
  194. output_high(PIN_B3);
  195. output_high(PIN_B4);
  196. output_high(PIN_B5);
  197. delay_ms(1000);
  198. output_low(PIN_B1); //Start test
  199. output_low(PIN_B2);
  200. output_low(PIN_B3);
  201. output_low(PIN_B4);
  202. output_low(PIN_B5);
  203. break;
  204. case '2':
  205. for(i=0;i<100;i++)
  206. {
  207. output_high(PIN_B1); //Start test
  208. output_high(PIN_B2);
  209. output_high(PIN_B3);
  210. output_high(PIN_B4);
  211. output_high(PIN_B5);
  212. delay_ms(150);
  213. output_low(PIN_B1); //Start test
  214. output_low(PIN_B2);
  215. output_low(PIN_B3);
  216. output_low(PIN_B4);
  217. output_low(PIN_B5);
  218. }
  219. break;
  220. } */
  221. restart_wdt();
  222.  
  223. switch(Dimming){
  224. case 0:
  225. for(i=0;i<100;i++) //Dimming 30% (T=10ms) f=100Hz
  226. {
  227. output_high(PIN_B1);
  228. output_high(PIN_B2);
  229. output_high(PIN_B3);
  230. output_high(PIN_B4);
  231. output_high(PIN_B5);
  232. delay_ms(4);
  233. output_low(PIN_B1);
  234. output_low(PIN_B2);
  235. output_low(PIN_B3);
  236. output_low(PIN_B4);
  237. output_low(PIN_B5);
  238. delay_ms(9);
  239. }
  240. break;
  241. case 1:
  242.  
  243. for(i=0;i<100;i++) //Dimming 20% (T=10ms) f=100Hz
  244. {
  245. output_high(PIN_B1);
  246. output_high(PIN_B2);
  247. output_high(PIN_B3);
  248. output_high(PIN_B4);
  249. output_high(PIN_B5);
  250. delay_ms(2);
  251. //delay_us(500);
  252. output_low(PIN_B1);
  253. output_low(PIN_B2);
  254. output_low(PIN_B3);
  255. output_low(PIN_B4);
  256. output_low(PIN_B5);
  257. delay_ms(8);
  258. //delay_us(500);
  259. }
  260. break;
  261. case 2:for(i=0;i<100;i++) //Dimming 10% (T=10ms) f=100Hz
  262. {
  263. output_high(PIN_B1);
  264. output_high(PIN_B2);
  265. output_high(PIN_B3);
  266. output_high(PIN_B4);
  267. output_high(PIN_B5);
  268. delay_ms(1);
  269. // delay_us(500);
  270. output_low(PIN_B1);
  271. output_low(PIN_B2);
  272. output_low(PIN_B3);
  273. output_low(PIN_B4);
  274. output_low(PIN_B5);
  275. delay_ms(9);
  276. // delay_us(500);
  277. }
  278. break;
  279. case 3:for(i=0;i<100;i++) //Dimming 5% (T=10ms) f=100Hz
  280. {
  281. output_high(PIN_B1);
  282. output_high(PIN_B2);
  283. output_high(PIN_B3);
  284. output_high(PIN_B4);
  285. output_high(PIN_B5);
  286. delay_ms(1);
  287. // delay_us(500);
  288. output_low(PIN_B1);
  289. output_low(PIN_B2);
  290. output_low(PIN_B3);
  291. output_low(PIN_B4);
  292. output_low(PIN_B5);
  293. delay_ms(9);
  294. // delay_us(500);
  295. }
  296. break;
  297. default: output_high(PIN_B1); //Dimming 100%
  298. output_high(PIN_B2);
  299. output_high(PIN_B3);
  300. output_high(PIN_B4);
  301. output_high(PIN_B5);
  302. delay_ms(1000);
  303. break;
  304.  
  305. }
  306.  
  307.  
  308.  
  309. }//fim while(true) 1
  310.  
  311.  
  312. }//fim main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement