📄 new text document.txt
字号:
//AD7705.c
//ATMEGA 16
//4 Mhz
#include <avr/io.h>
#include <stdlib.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include "uart.h"
#include "uart.c"
#ifndef F_CPU
#define F_CPU 4000000UL
#endif
#define UART_BAUD_RATE 9600
int main(void)
{
//uart initialization
uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
sei();
//Set MOSI, SS, and SCLK as output MISO and PB0 as input
DDRB = ((1<<5)|(1<<7)|(1<<4));
//Enable SPI, Select Master, SCLK idles high,
//Sample on rising edge Setup on falling edge,
//SCLK=F_CPU/4
SPCR = ((1<<SPE)|(1<<MSTR)|(1<<CPOL)|(1<<CPHA));
//Serially reset the Chip
SPDR = 0xFF;
//Wait for transfer
while(!(SPSR & (1<<SPIF)));
//Serially reset the Chip
SPDR = 0xFF;
//Wait for transfer
while(!(SPSR & (1<<SPIF)));
//Serially reset the Chip
SPDR = 0xFF;
//Wait for transfer
while(!(SPSR & (1<<SPIF)));
//Serially reset the Chip
SPDR = 0xFF;
//Wait for transfer
while(!(SPSR & (1<<SPIF)));
//Serially reset the Chip
SPDR = 0xFF;
//Wait for transfer
while(!(SPSR & (1<<SPIF)));
//Send to Comm Reg: Next Write Clk
SPDR = 0b00100000;
//Wait for transfer
while(!(SPSR & (1<<SPIF)));
//Send to ClkReg: Clock Bits and Update rate
SPDR = 0b00011000;
//Wait for transfer
while(!(SPSR & (1<<SPIF)));
//Send to Comm Reg: write setup
SPDR = 0b00010000;
//Wait for transfer
while(!(SPSR & (1<<SPIF)));
//Send to Setup: FSYNC, Gain, Conditions, Self Calibration
SPDR = 0b01000100;
//Wait for transfer
while(!(SPSR & (1<<SPIF)));
unsigned int adc=0;
unsigned int value = 0;
unsigned int number=0;
char buffer[16];
for(;;)
{
//Is DDRY pin low/data ready?
while(PORTB & 0x01);
//Send to Comm: next read Data
SPDR = 0b00111000;
//Wait for transfer
while(!(SPSR & (1<<SPIF)));
//Pump in Garbage
SPDR = 0;
//Wait for transfer
while(!(SPSR & (1<<SPIF)));
//Get high byte
adc = 0;
adc = SPDR;
//Pump in Garbage
SPDR = 0;
//Wait for transfer
while(!(SPSR & (1<<SPIF)));
//get low byte
value = 0;
value = SPDR;
//combine high and low byte for 16 bit
number = 0;
number = ((adc<<8)+value);
itoa(number,buffer,10); //convert number to string
uart_puts(buffer); //send adc string to PC
uart_putc(0x0D); //Sends return
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -