Skip to content

Instantly share code, notes, and snippets.

@monsonite
Created January 15, 2016 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monsonite/034a4a63099590d743d5 to your computer and use it in GitHub Desktop.
Save monsonite/034a4a63099590d743d5 to your computer and use it in GitHub Desktop.
// Txtzyme_UART_2
// A code-reduced version of Txtzyme for the Arduino
// Ken Boak 2013-2016
// Inspired by Txtzyme by Ward Cunningham
// https://github.com/WardCunningham/Txtzyme/blob/master/arduino/Arduinozyme/Arduinozyme.pde
// Replaced many of Arduino I/O dependencies reducing code from 3518 to 1040 bytes!
#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
// MACRO for a 62.5nS delay
#define NOP __asm__ __volatile__ ("nop\n\t") // and then use it in code as follows NOP; // delay 62.5ns on a 16MHz AtMega
#define F_CPU 16000000UL // define the clock frequency as 16MHz
#define BAUD 115200
#include <util/setbaud.h> // Set up the Uart baud rate generator
#include <uart.h>
unsigned int x = 0;
int d = 6; // On WiNode set LED pin as 6
//int num_val;
//int decade;
unsigned int i, j, w, y, z ;
//char num_buf[5];
main() { // We use a main() aand a while(1) to dispense with setup() and loop()
UBRR0H = UBRRH_VALUE; // Enable UART
UBRR0L = UBRRL_VALUE;
UCSR0A |= _BV(U2X0);
// UCSR0A &= ~(_BV(U2X0));
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
DDRD = DDRD | B11111100; // Sets pins 2 to 7 as outputs without changing the value of pins 0 & 1, which are RX & TX
DDRB = DDRB | B11111111; // Port B (Pin 9 - 13) is also output
// PORTB &= B11111110; // Set pin 8 low
OK();
while(1){
char buf[64];
txtRead(buf, 64);
txtEval(buf);
}
}
void txtRead (char *p, byte n) {
byte i = 0;
while (i < (n-1)) {
char ch = u_getchar();
if (ch == '\r' || ch == '\n') break;
if (ch >= ' ' && ch <= '~') {
*p++ = ch;
i++;
}
}
*p = 0;
}
void txtEval (char *buf) {
unsigned int k = 0;
char *loop;
char ch;
while ((ch = *buf++)) {
switch (ch) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
x = ch - '0';
while (*buf >= '0' && *buf <= '9') {
x = x*10 + (*buf++ - '0');
}
break;
case 'p':
printlong(x);
break;
case 'd':
d = x;
break;
// I/O Group
case 'h':
PORTD |= B01000000; // Set bit 6 high
break;
case 'l':
PORTD &= B10111111; // Set bit 6 low
break;
/*
case 'i':
x = digitalRead(d);
break;
*/
/*
case 'o':
digitalWrite(d, x%2);
break;
*/
case 'm':
delay_mS(x);
break;
case 'u':
delay_uS(x);
break;
case '{':
k = x;
loop = buf;
while ((ch = *buf++) && ch != '}') {
}
case '}':
if (k) {
k--;
buf = loop;
}
break;
case 'k':
x = k;
break;
case '_':
while ((ch = *buf++) && ch != '_') {
u_putchar(ch);
}
u_putchar(13);
break;
case 's':
x = analogRead(x);
break;
}
}
}
//--------------------------------------------------------------------------------------
// UART Routines
//--------------------------------------------------------------------------------------
void u_putchar(char c) {
loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */
UDR0 = c;
}
char u_getchar(void) {
loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */
return UDR0;
}
//-----------------------------------------------------------------------------------------
// Print a 16 bit int number followed by crlf - note recursive function for brevity
static void printlong(unsigned short num) {
if (num / (unsigned short)10 != 0) printlong(num / (unsigned short)10);
u_putchar((char)(num % (unsigned short)10) + '0');
crlf();
return;
}
//----------------------------------------------------------------------------------------------------------
// Print a string
void printstring(char *buf)
{
}
//----------------------------------------------------------------------------------------------------------
// Print a CR-LF
void crlf(void) // send a crlf
{
u_putchar(10);
u_putchar(13);
}
//---------------------------------------------------------------------------------------------------------
// Print OK
void OK(void) // send OK crlf
{
u_putchar('O');
u_putchar('K');
crlf();
}
//---------------------------------------------------------------------------------------------------------
// Generate a multiple uS delay
void delay_uS(int w)
{
int k;
for(k = 0; k <= w; k++)
{
/*
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
*/
NOP;
}
}
//---------------------------------------------------------------------------------------------------------
// Generate a multiple mS delay
void delay_mS(int z)
{
for(j = 0; j <= z; j++)
{
delay_uS(1000); // 1000 us delay
}
}
//---------------------------------------------------------------------------------------------------------
//------------------------------------That's All Folks!-------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment