Added shiftOut() routine and fixed new serial functions to work with the ATmega168.

This commit is contained in:
David A. Mellis 2006-11-23 19:13:21 +00:00
parent a740bf0588
commit bd17cfbdc0
2 changed files with 48 additions and 8 deletions

View File

@ -249,6 +249,17 @@ void analogWrite(int pin, int val)
void beginSerial(long baud) void beginSerial(long baud)
{ {
#if defined(__AVR_ATmega168__)
UBRR0H = ((F_CPU / 16 + baud / 2) / baud - 1) >> 8;
UBRR0L = ((F_CPU / 16 + baud / 2) / baud - 1);
// enable rx and tx
sbi(UCSR0B, RXEN0);
sbi(UCSR0B, TXEN0);
// enable interrupt on complete reception of a byte
sbi(UCSR0B, RXCIE0);
#else
UBRRH = ((F_CPU / 16 + baud / 2) / baud - 1) >> 8; UBRRH = ((F_CPU / 16 + baud / 2) / baud - 1) >> 8;
UBRRL = ((F_CPU / 16 + baud / 2) / baud - 1); UBRRL = ((F_CPU / 16 + baud / 2) / baud - 1);
@ -258,6 +269,7 @@ void beginSerial(long baud)
// enable interrupt on complete reception of a byte // enable interrupt on complete reception of a byte
sbi(UCSRB, RXCIE); sbi(UCSRB, RXCIE);
#endif
// defaults to 8-bit, no parity, 1 stop bit // defaults to 8-bit, no parity, 1 stop bit
} }
@ -294,9 +306,18 @@ int serialRead()
} }
} }
#if defined(__AVR_ATmega168__)
SIGNAL(SIG_USART_RECV)
#else
SIGNAL(SIG_UART_RECV) SIGNAL(SIG_UART_RECV)
#endif
{ {
#if defined(__AVR_ATmega168__)
unsigned char c = UDR0;
#else
unsigned char c = UDR; unsigned char c = UDR;
#endif
int i = (rx_buffer_head + 1) % RX_BUFFER_SIZE; int i = (rx_buffer_head + 1) % RX_BUFFER_SIZE;
// if we should be storing the received character into the location // if we should be storing the received character into the location
@ -503,6 +524,20 @@ unsigned long pulseIn(int pin, int state)
return width * (16000000UL / F_CPU) * 20 / 23; return width * (16000000UL / F_CPU) * 20 / 23;
} }
void shiftOut(int dataPin, int clockPin, int bitOrder, byte val) {
int i;
for (i = 0; i < 8; i++) {
if (bitOrder == LSBFIRST)
digitalWrite(dataPin, !!(val & (1 << i)));
else
digitalWrite(dataPin, !!(val & (1 << (7 - i))));
digitalWrite(clockPin, HIGH);
digitalWrite(clockPin, LOW);
}
}
int main(void) int main(void)
{ {
// this needs to be called before setup() or some functions won't // this needs to be called before setup() or some functions won't

View File

@ -48,6 +48,9 @@ extern "C"{
#define SERIAL 0x0 #define SERIAL 0x0
#define DISPLAY 0x1 #define DISPLAY 0x1
#define LSBFIRST 0
#define MSBFIRST 1
#define min(a,b) ((a)<(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b))
#define abs(x) ((x)>0?(x):-(x)) #define abs(x) ((x)>0?(x):-(x))
@ -84,6 +87,8 @@ void delay(unsigned long);
void delayMicroseconds(unsigned int us); void delayMicroseconds(unsigned int us);
unsigned long pulseIn(int pin, int state); unsigned long pulseIn(int pin, int state);
void shiftOut(int dataPin, int clockPin, int endianness, byte val);
void setup(void); void setup(void);
void loop(void); void loop(void);