File: StdDefs.c

package info (click to toggle)
simulavr 1.0.0%2Bgit20160221.e53413b-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 5,748 kB
  • sloc: cpp: 35,491; python: 6,991; ansic: 3,567; makefile: 1,072; sh: 653; asm: 414; tcl: 320
file content (142 lines) | stat: -rw-r--r-- 2,829 bytes parent folder | download | duplicates (2)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 *  $Id$
 */

// Common routines used in most programs

#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <string.h>
#include <compat/deprecated.h>

#include "StdDefs.h"
#include "serial.h"

// 02-Sep-2008 K. Schwichtenberg _delay_ms included for a useful timing

void setbaud(BaudRate br)
   {
   UART_BAUD_REG = br;
   }

// Change State every on or off calls to run_led
// Define on time and off time
void run_led(INT16U ontime, INT16U offtime)
    {
	static int c;
   static int x;

   if (c > x)
	   {
	   if (bit_is_set(RUNLED_PORT, RUNLED_BIT))
         {
         cbi(RUNLED_PORT, RUNLED_BIT);
         x = offtime;
         }
	   else
	 	  {
         sbi(RUNLED_PORT, RUNLED_BIT);
         x = ontime;
         }
	   c = 0;
	   }
	else
	   c++;
    }

// Toggles the test pin every call
void test_pin(void)
   {
   if (bit_is_set(TESTPIN_PORT, TESTPIN_BIT))
       cbi(TESTPIN_PORT, TESTPIN_BIT);
   else
       sbi(TESTPIN_PORT, TESTPIN_BIT);
   }

// Delay in 1/10's of a millisecond
// Does not work with -O0, use -O1, even for debugging.
void msleep(INT16U ms10)
    {
        for( ; ms10; --ms10)
            _delay_ms(0.1);
    }

//------------------------------------------------------------
// void putBCD(INT16S X, CHARU length, CHARU TrailingSpace)
//
// Outputs to UART a signed integer represented as a decimal integer
// up to 5 digits long plus negative sign plus trailing space (-)DDDD.
// Prefixes a - sign if number is negative.
// If TrailingSpace is not equal to 0 a trailing space is appended
// Length defines number of characters printed including - sign and
// trailing space.

void putBCD(INT16S X, CHARU length, CHARU TrailingSpace)

	 {
	  CHARU byte1, byte2, byte3, byte4, byte5;

     if (TrailingSpace) length --;

     if (X < 0)
       {
       X = X * (-1);
       putchar('-');
       length --;
       }

	  byte1 = (CHARU)(X % 10) + 0x30;
	  X /= 10;
	  byte2 = (CHARU)(X % 10) + 0x30;
	  X /= 10;
	  byte3 = (CHARU)(X % 10) + 0x30;
	  X /= 10;
	  byte4 = (CHARU)(X % 10) + 0x30;
	  X /= 10;
	  byte5 = (CHARU)(X % 10) + 0x30;

	  if (length > 4) putchar(byte5);
	  if (length > 3) putchar(byte4);
	  if (length > 2) putchar(byte3);
	  if (length > 1) putchar(byte2);
	  putchar(byte1);
     if (TrailingSpace) putchar(' ');

    }
/*
void putchar(CHARU c)
   {
   while(bit_is_clear(UART_STATUS_REG, 5));
   UART_DATA_REG = c;
   }
*/

void putstr(CHARU *s)
   {
   int j;
   for (j = 0; j <= strlen((char*)s); j++)
       putchar(*(s + j));
   CRLF();
   }

void print_hexbyte(unsigned char i)
{
    unsigned char h, l;

    h = i & 0xF0;               // High nibble
    h = h>>4;
    h = h + '0';

    if (h > '9')
        h = h + 7;

    l = (i & 0x0F)+'0';         // Low nibble
    if (l > '9')
        l = l + 7;


    putchar(h);
    putchar(l);
}