/[H8]/trunk/PIC/Demo trimmet/TFTPcDemo.c
ViewVC logotype

Annotation of /trunk/PIC/Demo trimmet/TFTPcDemo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 111 - (hide annotations) (download)
Mon May 28 15:00:55 2007 UTC (16 years, 11 months ago) by hedin
File MIME type: text/plain
File size: 9556 byte(s)
embedded 1.0 rc1
1 hedin 91 #define THIS_IS_STACK_APPLICATION
2    
3     #include "stacktsk.h"
4     #include "tick.h"
5     #include "delay.h"
6 hedin 106 #include "network.h"
7 hedin 109 #include "barcode.h"
8 hedin 111 #include "keyboard.h"
9     #include "MyI2C.h"
10     #include "xlcd.h"
11 hedin 105 #include <string.h>
12    
13 hedin 91 /*
14     * This is used by other stack elements.
15     * Main application must define this and initialize it with
16     * proper values.
17     */
18     APP_CONFIG AppConfig;
19    
20    
21     BYTE myDHCPBindCount = 0;
22     #if defined(STACK_USE_DHCP)
23     extern BYTE DHCPBindCount;
24     #else
25     /*
26     * If DHCP is not enabled, force DHCP update.
27     */
28     BYTE DHCPBindCount = 1;
29     #endif
30    
31     /*
32     * Set configuration fuses for HITECH compiler.
33     * For MCC18 compiler, separately linked config.asm file
34     * will set the correct fuses.
35     */
36     #if defined(HITECH_C18)
37     __CONFIG(1, UNPROTECT & HS);
38     __CONFIG(2, PWRTEN & BORDIS & WDTDIS);
39     #endif
40    
41    
42     /*
43     * Private helper functions.
44     * These may or may not be present in all applications.
45     */
46     static void InitAppConfig(void);
47     static void InitializeBoard(void);
48     static void SetConfig(void);
49    
50 hedin 111 // Skal fjernes
51     #define LCD_IC 0x74
52     void mylcd_write_byte(unsigned char data)
53     {
54     StartI2C(LCD_IC);
55     WriteI2CByte( data );
56     StopI2C();
57     Delay10us(1); //I2C spec's siger min 4.7 uSec mellem stop og næste start
58     }
59 hedin 96
60    
61 hedin 111
62 hedin 91 void main(void)
63     {
64     // Tick to blink SYSTEM led.
65     static TICK t = 0;
66 hedin 111 char i;
67     unsigned char numbers[20];
68     unsigned long amount;
69     unsigned char lcd_buffer[21];
70     short number_count = 0;
71     short produkt_amount = 0;
72     static bit hello = 0;
73     short varetal = 1;
74 hedin 91
75 hedin 93 TICK tick;
76     TICK lastTick;
77     TICK diffTicks;
78 hedin 91
79     /*
80     * Initialize any application specific hardware.
81     */
82     InitializeBoard();
83    
84     /*
85     * Initialize all stack related components.
86     * Following steps must be performed for all applications using
87     * PICmicro TCP/IP Stack.
88     */
89     TickInit();
90    
91     /*
92     * Initialize Stack and application related NV variables.
93     */
94     InitAppConfig();
95    
96     StackInit();
97 hedin 96
98     network_init();
99 hedin 111 barcode_init();
100     InitI2C();
101     XLCDInit();
102    
103 hedin 91
104    
105     /*
106     * Once all items are initialized, go into infinite loop and let
107     * stack items execute their tasks.
108     * If application needs to perform its own task, it should be
109     * done at the end of while loop.
110     * Note that this is a "co-operative mult-tasking" mechanism
111     * where every task performs its tasks (whether all in one shot
112     * or part of it) and returns so that other tasks can do their
113     * job.
114     * If a task needs very long time to do its job, it must broken
115     * down into smaller pieces so that other tasks can have CPU time.
116     */
117    
118    
119 hedin 108 // Defines the IP add. on the server machine (rnode is defined in network.h)
120 hedin 91 rnode.IPAddr.v[0] = 192;
121     rnode.IPAddr.v[1] = 168;
122     rnode.IPAddr.v[2] = 1;
123     rnode.IPAddr.v[3] = 20;
124    
125     while(1)
126     {
127 hedin 111 char keyboard_entered;
128     char network_error;
129    
130    
131 hedin 91 /*
132     * Blink SYSTEM LED every second.
133     */
134     if ( TickGetDiff(TickGet(), t) >= TICK_SECOND/2 )
135     {
136     t = TickGet();
137     LATA4 ^= 1;
138     }
139    
140     /*
141     * This task performs normal stack task including checking
142     * for incoming packet, type of packet and calling
143     * appropriate stack entity to process it.
144     */
145     StackTask();
146    
147     /*
148     * For DHCP information, display how many times we have renewed the IP
149     * configuration since last reset.
150     */
151 hedin 111
152     //mylcd_write_byte(0b10100101);
153    
154     if( (barcode_is_ready() || keyboard_entered) && !hello){
155     network_send_hello(0x01);
156     hello = 1;
157     }
158     if (barcode_is_ready())
159     {
160     for (i=0;i< barcode_get_length(); i++)
161     {
162     global_network_char[i] = global_barcode_buffer[i];
163     global_network_charlen = barcode_get_length();
164     }
165     barcode_reset();
166     network_send_scan_frame( varetal, global_network_charlen );
167     varetal = 1;
168     /*
169     XLCDClear();
170     XLCDGoto(1,0);
171     for (i=0;i< barcode_get_length(); i++)
172     {
173     XLCDPut(global_barcode_buffer[i]);
174     }
175     */
176     }
177    
178     keyboard_entered = keyboard_read_safe();
179     if( keyboard_entered )
180     {
181     switch( keyboard_entered )
182     {
183     case 'A':
184     network_send_calc_total();
185     XLCDClear();
186     XLCDGoto(0,0);
187     for( i=0; i<global_network_charlen; i++ )
188     {
189     XLCDPut(global_network_char[i]);
190     }
191     break;
192    
193     case 'B':
194     amount = atoi(numbers);
195     number_count = 0;
196    
197     network_send_cash_payed(amount);
198     network_send_goodbye();
199     hello = 0;
200    
201     sprintf(lcd_buffer,"%-5d", global_network_amount);
202     XLCDGoto(0,0);
203     XLCDPutString(lcd_buffer);
204     break;
205    
206     case 'C':
207     network_send_cancel_last();
208     break;
209    
210     case 'D':
211     network_send_cancel_all();
212     break;
213    
214     case 'E':
215     for( i=0; i<number_count;i++ )
216     {
217     global_network_char[i] = numbers[i];
218     }
219     network_send_scan_frame( varetal, number_count );
220     varetal = 1;
221     number_count = 0;
222     break;
223    
224     case 'F':
225     numbers[number_count] = 0; //nulterminering inden brug af atoi
226     varetal = atoi(numbers);
227     number_count = 0;
228     break;
229    
230     default:
231     numbers[number_count] = keyboard_entered;
232    
233     //numbers; // got to catch 0-9; 20 byte array.
234     number_count++; // amount of used bytes in numbers.
235     }
236    
237     }
238     /*
239     network_send_hello( 0x01 );
240    
241     strncpy(global_network_char, "12345678", 8);
242 hedin 105 network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8
243    
244     strncpy(global_network_char, "87654444", 8);
245     network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8
246 hedin 111
247 hedin 105 network_send_calc_total();
248    
249     network_send_cash_payed(50000);
250    
251     network_send_cancel_last();
252    
253     network_send_cancel_all();
254    
255     network_send_goodbye();
256 hedin 111 */
257 hedin 91 }
258     }
259    
260 hedin 102 void interrupt HighISR(void)
261 hedin 91 {
262 hedin 109 if (RCIF == 1)
263     barcode_interrupt();
264     else
265     TickUpdate();
266 hedin 91 }
267    
268    
269 hedin 106
270 hedin 91 /*********************************************************************
271     * Function: void InitializeBoard(void)
272     *
273     * PreCondition: None
274     *
275     * Input: None
276     *
277     * Output: None
278     *
279     * Side Effects: None
280     *
281     * Overview: Initialize board specific hardware.
282     *
283     * Note: None
284     ********************************************************************/
285     static void InitializeBoard(void)
286     {
287     /*
288     * Setup for PORTA.RA0 as analog input while rests
289     * as digital i/o lines.
290     */
291     ADCON1 = 0b10001110; // RA0 as analog input, Right justified
292     TRISA = 0x03;
293    
294     /*
295     * LCD is enabled using RA5.
296     */
297 hedin 96 // PORTA_RA5 = 0; // Disable LCD.
298 hedin 91
299     /*
300     * Turn off the LED's.
301     */
302     LATA2 = 1;
303     LATA3 = 1;
304    
305     /*
306     * External data EEPROM needs pull-ups, so enable internal
307     * pull-ups.
308     */
309     INTCON2_RBPU = 0;
310    
311     T0CON = 0;
312     INTCON_GIEH = 1;
313     INTCON_GIEL = 1;
314    
315     }
316    
317     /*********************************************************************
318     * Function: void InitAppConfig(void)
319     *
320     * PreCondition: MPFSInit() is already called.
321     *
322     * Input: None
323     *
324     * Output: Write/Read non-volatile config variables.
325     *
326     * Side Effects: None
327     *
328     * Overview: None
329     *
330     * Note: None
331     ********************************************************************/
332     static void InitAppConfig(void)
333     {
334     /*
335     * Load default configuration into RAM.
336     */
337     AppConfig.MyIPAddr.v[0] = MY_DEFAULT_IP_ADDR_BYTE1;
338     AppConfig.MyIPAddr.v[1] = MY_DEFAULT_IP_ADDR_BYTE2;
339     AppConfig.MyIPAddr.v[2] = MY_DEFAULT_IP_ADDR_BYTE3;
340     AppConfig.MyIPAddr.v[3] = MY_DEFAULT_IP_ADDR_BYTE4;
341    
342     AppConfig.MyMask.v[0] = MY_DEFAULT_MASK_BYTE1;
343     AppConfig.MyMask.v[1] = MY_DEFAULT_MASK_BYTE2;
344     AppConfig.MyMask.v[2] = MY_DEFAULT_MASK_BYTE3;
345     AppConfig.MyMask.v[3] = MY_DEFAULT_MASK_BYTE4;
346    
347     AppConfig.MyGateway.v[0] = MY_DEFAULT_GATE_BYTE1;
348     AppConfig.MyGateway.v[1] = MY_DEFAULT_GATE_BYTE2;
349     AppConfig.MyGateway.v[2] = MY_DEFAULT_GATE_BYTE3;
350     AppConfig.MyGateway.v[3] = MY_DEFAULT_GATE_BYTE4;
351    
352     AppConfig.MyMACAddr.v[0] = MY_DEFAULT_MAC_BYTE1;
353     AppConfig.MyMACAddr.v[1] = MY_DEFAULT_MAC_BYTE2;
354     AppConfig.MyMACAddr.v[2] = MY_DEFAULT_MAC_BYTE3;
355     AppConfig.MyMACAddr.v[3] = MY_DEFAULT_MAC_BYTE4;
356     AppConfig.MyMACAddr.v[4] = MY_DEFAULT_MAC_BYTE5;
357     AppConfig.MyMACAddr.v[5] = MY_DEFAULT_MAC_BYTE6;
358    
359     #if defined(STACK_USE_DHCP)
360     AppConfig.Flags.bIsDHCPEnabled = TRUE;
361     #else
362     AppConfig.Flags.bIsDHCPEnabled = FALSE;
363     #endif
364     }
365    
366     BOOL StringToIPAddress(char *str, IP_ADDR *buffer)
367     {
368     BYTE v;
369     char *temp;
370     BYTE byteIndex;
371    
372     temp = str;
373     byteIndex = 0;
374    
375     while( v = *str )
376     {
377     if ( v == '.' )
378     {
379     *str++ = '\0';
380     buffer->v[byteIndex++] = atoi(temp);
381     temp = str;
382     }
383     else if ( v < '0' || v > '9' )
384     return FALSE;
385    
386     str++;
387     }
388    
389     buffer->v[byteIndex] = atoi(temp);
390    
391     return (byteIndex == 3);
392     }
393    
394 hedin 111 void XLCDDelay15ms(void)
395     {
396     DelayMs(15);
397     }
398     void XLCDDelay4ms(void)
399     {
400     DelayMs(4);
401     }
402    
403     void XLCDDelay100us(void)
404     {
405     INTCON_GIEH = 0;
406     Delay10us(1);
407     INTCON_GIEH = 1;
408     }

  ViewVC Help
Powered by ViewVC 1.1.20