Advertisement
Guest User

Untitled

a guest
Jan 9th, 2015
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.00 KB | None | 0 0
  1. /*
  2. * FreeModbus Libary: AVR Demo Application
  3. * Copyright (C) 2006 Christian Walter <wolti@sil.at>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * File: $Id: excoils.c,v 1.7 2006/09/17 16:46:33 wolti Exp $
  20. */
  21.  
  22.  
  23.  
  24. /* ----------------------- Defines ------------------------------------------*/
  25. #define REG_DISC_START 1000
  26. #define REG_DISC_SIZE 16
  27. #define REG_COILS_START 1000
  28. #define REG_COILS_SIZE 16
  29. #define FALSE 0
  30. #define TRUE 1
  31.  
  32. /* ----------------------- Modbus includes ----------------------------------*/
  33. #include "mb.h"
  34. #include "mbutils.h"
  35.  
  36. #include <avr/io.h>
  37. #include "util/delay.h"
  38. #include "24c64.h"
  39. /* ----------------------- Static variables ---------------------------------*/
  40. static unsigned char ucRegDiscBuf[REG_DISC_SIZE / 8];
  41. static unsigned char ucRegCoilsBuf[REG_COILS_SIZE / 8];
  42. /* ----------------------- Start implementation -----------------------------*/
  43.  
  44.  
  45. void Wait()
  46. {
  47. uint8_t i;
  48.  
  49. for(i=0;i<100;i++)
  50. _delay_loop_2(0);
  51. }
  52.  
  53.  
  54. int main (void)
  55. {
  56. DDRB = 0xff;
  57. DDRC = 0xff;
  58. PORTD = 0x00;
  59. DDRD = 0x00;
  60. PORTA = 0x00;
  61. DDRA = 0x00;
  62.  
  63. uint8_t failed;
  64. unsigned int address;
  65.  
  66. EEOpen();
  67.  
  68.  
  69. failed=0;
  70. for(address=0;address<8192;address++)
  71. {
  72. if(EEWriteByte(address,7)==0)
  73. {
  74. //Write Failed
  75. failed=1;
  76. Wait();
  77. break;
  78. }
  79. }
  80.  
  81. /* Select either ASCII or RTU Mode. */
  82. ( void )eMBInit( MB_RTU, 0x0A, 0, 9600, MB_PAR_NONE );
  83.  
  84.  
  85. /* if (EEWriteByte(address,writeByte)) {
  86. PORTB |= ledPin;
  87. _delay_ms(1000);
  88. PORTB &= ~ledPin;
  89. _delay_ms(1000);
  90. }
  91. */
  92.  
  93. /* Enable the Modbus Protocol Stack. */
  94. ( void )eMBEnable( );
  95.  
  96. for( ;; )
  97. {
  98. /* Call the main polling loop of the Modbus protocol stack. */
  99.  
  100.  
  101.  
  102.  
  103. /* Check if we have registers mapped at this block. */
  104.  
  105. ( void )eMBPoll( );
  106.  
  107. if (xMBUtilGetBits( ucRegCoilsBuf, 0, 1 )==1)
  108. {
  109.  
  110. PORTB |= _BV(PB0);
  111.  
  112. }else
  113.  
  114. {
  115. PORTB &= ~_BV(PB0);
  116. }
  117.  
  118. if (xMBUtilGetBits( ucRegCoilsBuf, 1, 1 )==1)
  119. {
  120. PORTB |= _BV(PB1);
  121.  
  122. }else
  123.  
  124. {
  125. PORTB &= ~_BV(PB1);
  126. }
  127. if (xMBUtilGetBits( ucRegCoilsBuf, 2, 1 )==1)
  128. {
  129. PORTB |= _BV(PB2);
  130. }else
  131.  
  132. {
  133. PORTB &= ~_BV(PB2);
  134. }
  135. if (xMBUtilGetBits( ucRegCoilsBuf, 3, 1 )==1)
  136. {
  137. PORTB |= _BV(PB3);
  138. }else
  139.  
  140. {
  141. PORTB &= ~_BV(PB3);
  142. }
  143.  
  144. if (xMBUtilGetBits( ucRegCoilsBuf, 4, 1 )==1)
  145. {
  146. PORTB |= _BV(PB4);
  147. }else
  148.  
  149. {
  150. PORTB &= ~_BV(PB4);
  151. }
  152.  
  153. if (xMBUtilGetBits( ucRegCoilsBuf, 5, 1 )==1)
  154. {
  155. PORTB |= _BV(PB5);
  156. }else
  157.  
  158. {
  159. PORTB &= ~_BV(PB5);
  160. }
  161.  
  162. if (xMBUtilGetBits( ucRegCoilsBuf, 6, 1 )==1)
  163. {
  164. PORTB |= _BV(PB6);
  165. }else
  166.  
  167. {
  168. PORTB &= ~_BV(PB6);
  169. }
  170.  
  171. if (xMBUtilGetBits( ucRegCoilsBuf, 7, 1 )==1)
  172. {
  173. PORTB |= _BV(PB7);
  174. }else
  175.  
  176. {
  177. PORTB &= ~_BV(PB7);
  178. }
  179.  
  180. if (xMBUtilGetBits( ucRegCoilsBuf, 8, 1 )==1)
  181. {
  182.  
  183. PORTC |= _BV(PC0);
  184.  
  185. }else
  186.  
  187. {
  188. PORTC &= ~_BV(PC0);
  189. }
  190.  
  191. if (xMBUtilGetBits( ucRegCoilsBuf, 9, 1 )==1)
  192. {
  193. PORTC |= _BV(PC1);
  194.  
  195. }else
  196.  
  197. {
  198. PORTC &= ~_BV(PC1);
  199. }
  200. if (xMBUtilGetBits( ucRegCoilsBuf, 10, 1 )==1)
  201. {
  202. PORTC |= _BV(PC2);
  203. }else
  204.  
  205. {
  206. PORTC &= ~_BV(PC2);
  207. }
  208. if (xMBUtilGetBits( ucRegCoilsBuf, 11, 1 )==1)
  209. {
  210. PORTC |= _BV(PC3);
  211. }else
  212.  
  213. {
  214. PORTC &= ~_BV(PC3);
  215. }
  216.  
  217. if (xMBUtilGetBits( ucRegCoilsBuf, 12, 1 )==1)
  218. {
  219. PORTC |= _BV(PC4);
  220. }else
  221.  
  222. {
  223. PORTC &= ~_BV(PC4);
  224. }
  225.  
  226. if (xMBUtilGetBits( ucRegCoilsBuf, 13, 1 )==1)
  227. {
  228. PORTC |= _BV(PC5);
  229. }else
  230.  
  231. {
  232. PORTC &= ~_BV(PC5);
  233. }
  234.  
  235. if (xMBUtilGetBits( ucRegCoilsBuf, 14, 1 )==1)
  236. {
  237. PORTC |= _BV(PC6);
  238. }else
  239.  
  240. {
  241. PORTC &= ~_BV(PC6);
  242. }
  243.  
  244. if (xMBUtilGetBits( ucRegCoilsBuf, 15, 1 )==1)
  245. {
  246. PORTC |= _BV(PC7);
  247. }else
  248.  
  249. {
  250. PORTC &= ~_BV(PC7);
  251. }
  252.  
  253.  
  254. }
  255. }
  256.  
  257. eMBErrorCode
  258. eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils,
  259. eMBRegisterMode eMode )
  260. {
  261. eMBErrorCode eStatus = MB_ENOERR;
  262. int iNCoils = ( int )usNCoils;
  263. unsigned short usBitOffset;
  264.  
  265. /* Check if we have registers mapped at this block. */
  266. if( ( usAddress >= REG_COILS_START ) &&
  267. ( usAddress + usNCoils <= REG_COILS_START + REG_COILS_SIZE ) )
  268. {
  269. usBitOffset = ( unsigned short )( usAddress - REG_COILS_START );
  270. switch ( eMode )
  271. {
  272. /* Read current values and pass to protocol stack. */
  273. case MB_REG_READ:
  274. while( iNCoils > 0 )
  275. {
  276. *pucRegBuffer++ =
  277. xMBUtilGetBits( ucRegCoilsBuf, usBitOffset,
  278. ( unsigned char )( iNCoils >
  279. 8 ? 8 :
  280. iNCoils ) );
  281. iNCoils -= 8;
  282. usBitOffset += 8;
  283. }
  284. break;
  285.  
  286. /* Update current register values. */
  287. case MB_REG_WRITE:
  288. while( iNCoils > 0 )
  289. {
  290. xMBUtilSetBits( ucRegCoilsBuf, usBitOffset,
  291. ( unsigned char )( iNCoils > 8 ? 8 : iNCoils ),
  292. *pucRegBuffer++ );
  293. iNCoils -= 8;
  294. usBitOffset += 8;
  295. }
  296. break;
  297. }
  298.  
  299. }
  300. else
  301. {
  302. eStatus = MB_ENOREG;
  303. }
  304. return eStatus;
  305. }
  306.  
  307.  
  308. eMBErrorCode
  309. eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
  310. {
  311. return MB_ENOREG;
  312. }
  313.  
  314.  
  315. eMBErrorCode
  316. eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs,
  317. eMBRegisterMode eMode )
  318. {
  319. return MB_ENOREG;
  320. }
  321.  
  322. eMBErrorCode
  323. eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
  324.  
  325.  
  326. {
  327.  
  328. eMBErrorCode eStatus = MB_ENOERR;
  329. short iNDiscrete = ( short )usNDiscrete;
  330. unsigned short usBitOffset;
  331.  
  332.  
  333. if (bit_is_clear(PIND,0))
  334. {
  335. xMBUtilSetBits( ucRegDiscBuf, 0, 1, 1 );
  336. }
  337. else
  338. {
  339. xMBUtilSetBits( ucRegDiscBuf, 0, 1, 0 );
  340. }
  341.  
  342. if (bit_is_clear(PIND,1))
  343. {
  344. xMBUtilSetBits( ucRegDiscBuf, 1, 1, 1 );
  345. }
  346. else
  347. {
  348. xMBUtilSetBits( ucRegDiscBuf, 1, 1, 0 );
  349. }
  350.  
  351. if (bit_is_clear(PIND,2))
  352. {
  353. xMBUtilSetBits( ucRegDiscBuf, 2, 1, 1 );
  354. }
  355. else
  356. {
  357. xMBUtilSetBits( ucRegDiscBuf, 2, 1, 0 );
  358. }
  359.  
  360. if (bit_is_clear(PIND,3))
  361. {
  362. xMBUtilSetBits( ucRegDiscBuf, 3, 1, 1 );
  363. }
  364. else
  365. {
  366. xMBUtilSetBits( ucRegDiscBuf, 3, 1, 0 );
  367. }
  368.  
  369. if (bit_is_clear(PIND,4))
  370. {
  371. xMBUtilSetBits( ucRegDiscBuf, 4, 1, 1 );
  372. }
  373. else
  374. {
  375. xMBUtilSetBits( ucRegDiscBuf, 4, 1, 0 );
  376. }
  377.  
  378. if (bit_is_clear(PIND,5))
  379. {
  380. xMBUtilSetBits( ucRegDiscBuf, 5, 1, 1 );
  381. }
  382. else
  383. {
  384. xMBUtilSetBits( ucRegDiscBuf, 5, 1, 0 );
  385. }
  386.  
  387. if (bit_is_clear(PIND,6))
  388. {
  389. xMBUtilSetBits( ucRegDiscBuf, 6, 1, 1 );
  390. }
  391. else
  392. {
  393. xMBUtilSetBits( ucRegDiscBuf, 6, 1, 0 );
  394. }
  395.  
  396. if (bit_is_clear(PIND,7))
  397. {
  398. xMBUtilSetBits( ucRegDiscBuf, 7, 1, 1 );
  399. }
  400. else
  401. {
  402. xMBUtilSetBits( ucRegDiscBuf, 7, 1, 0 );
  403. }
  404. if (bit_is_clear(PINA,0))
  405. {
  406. xMBUtilSetBits( ucRegDiscBuf, 8, 1, 1 );
  407. }
  408. else
  409. {
  410. xMBUtilSetBits( ucRegDiscBuf, 8, 1, 0 );
  411. }
  412.  
  413. if (bit_is_clear(PINA,1))
  414. {
  415. xMBUtilSetBits( ucRegDiscBuf, 9, 1, 1 );
  416. }
  417. else
  418. {
  419. xMBUtilSetBits( ucRegDiscBuf, 9, 1, 0 );
  420. }
  421.  
  422. if (bit_is_clear(PINA,2))
  423. {
  424. xMBUtilSetBits( ucRegDiscBuf, 10, 1, 1 );
  425. }
  426. else
  427. {
  428. xMBUtilSetBits( ucRegDiscBuf, 10, 1, 0 );
  429. }
  430.  
  431. if (bit_is_clear(PINA,3))
  432. {
  433. xMBUtilSetBits( ucRegDiscBuf, 11, 1, 1 );
  434. }
  435. else
  436. {
  437. xMBUtilSetBits( ucRegDiscBuf, 11, 1, 0 );
  438. }
  439.  
  440. if (bit_is_clear(PINA,4))
  441. {
  442. xMBUtilSetBits( ucRegDiscBuf, 12, 1, 1 );
  443. }
  444. else
  445. {
  446. xMBUtilSetBits( ucRegDiscBuf, 12, 1, 0 );
  447. }
  448.  
  449. if (bit_is_clear(PINA,5))
  450. {
  451. xMBUtilSetBits( ucRegDiscBuf, 13, 1, 1 );
  452. }
  453. else
  454. {
  455. xMBUtilSetBits( ucRegDiscBuf, 13, 1, 0 );
  456. }
  457.  
  458. if (bit_is_clear(PINA,6))
  459. {
  460. xMBUtilSetBits( ucRegDiscBuf, 14, 1, 1 );
  461. }
  462. else
  463. {
  464. xMBUtilSetBits( ucRegDiscBuf, 14, 1, 0 );
  465. }
  466.  
  467. if (bit_is_clear(PINA,7))
  468. {
  469. xMBUtilSetBits( ucRegDiscBuf, 15, 1, 1 );
  470. }
  471. else
  472. {
  473. xMBUtilSetBits( ucRegDiscBuf, 15, 1, 0 );
  474. }
  475.  
  476. if( ( usAddress >= REG_DISC_START ) &&
  477. ( usAddress + usNDiscrete <= REG_DISC_START + REG_DISC_SIZE ) )
  478. {
  479. usBitOffset = ( unsigned short )( usAddress - REG_DISC_START );
  480. while( iNDiscrete > 0 )
  481. {
  482. *pucRegBuffer++ =
  483. xMBUtilGetBits( ucRegDiscBuf, usBitOffset,
  484. ( unsigned char )( iNDiscrete >
  485. 8 ? 8 : iNDiscrete ) );
  486. iNDiscrete -= 8;
  487. usBitOffset += 8;
  488. }
  489. }
  490. else
  491. {
  492. eStatus = MB_ENOREG;
  493. }
  494. return eStatus;
  495. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement