File: kuttyPy.c

package info (click to toggle)
expeyes 5.3.4%2Brepack-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 43,612 kB
  • sloc: python: 36,039; ansic: 9,754; xml: 1,010; makefile: 784; sh: 581; asm: 202; javascript: 46; php: 1
file content (54 lines) | stat: -rw-r--r-- 1,285 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* 
Program : kuttyPy.c
author  : Ajith Kumar (bpajith@gmail.com)
License : GNU GPL version 3 or above
A program to read/write the microcontroller registers from Python running on PC
*/

#include <avr/io.h>

#define VERSION   99     
#define GETVER    1

#define READB     2
#define WRITEB    3

int main (void)
{
uint8_t cmd, data, *port;

  // Initialize the RS232 communication link to the PC 38400, 8, 1, N
  UCSRB = (1 << RXEN) | (1 << TXEN);
  UBRRH = 0;
  UBRRL = 12;							// At 8MHz clock (12 =>38400 baudrate)
  UCSRC = (1 <<URSEL) | (1 << UCSZ1) | (1 << UCSZ0); // 8,1,N


  for(;;)			 					       // Infinite loop waiting for commands from PC
    {
    while ( !(UCSRA & (1<<RXC)) ) ;		// wait for command from PC
    cmd = UDR;							// Store the received byte 

    if(cmd == GETVER)
		{
		UDR = VERSION;
		}
    else if(cmd == READB)
        {
        while ( !(UCSRA & (1<<RXC)) ) ;		// wait
		port = UDR;							// get the port address		
		UDR  = *port;
		}
	else if(cmd == WRITEB)
		{
		while ( !(UCSRA & (1<<RXC)) ) ;		// wait
		port = UDR;							// get the register address		
		while ( !(UCSRA & (1<<RXC)) ) ;		// wait
		data = UDR;							// get the data
		*port = data;						// write data to the port address
		}

	// Invalid commands are ignored silently
    }
}