📄 notepad.c
字号:
// --PORTD
#define EN 2
#define RW 1
#define RS 0
#define lcd_rs_set() setb(PORTD,RS)
#define lcd_rs_clr() clrb(PORTD,RS)
#define lcd_en_set() setb(PORTD,EN)
#define lcd_en_clr() clrb(PORTD,EN)
#define lcd_rw_set() setb(PORTD,RW)
#define lcd_rw_clr() clrb(PORTD,RW)
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#define outp( val, port ) ( port ) = ( val )
#define inp( port ) ( port )
#define sbi( port, bit ) ( port ) |= ( 1 << (bit) )
#define cbi( port, bit ) ( port ) &= ~( 1 << (bit) )
#define chkbit( pin,bit ) (pin & (0x00 | ( 1 << (bit) )))
#define K_DELAY F_CPU/300
volatile char tempPort;
static volatile unsigned char Key;
static volatile unsigned int Key_BUFFER[12];
static char KeyEncoder[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
extern void delayloop(unsigned long t);
extern void delay_us(unsigned int us);
extern void delay_ms(unsigned long ms);
extern void wait_until_key_pressed(void);
extern void wait_until_key_free(void);
extern void blink(void);
extern void int0_initial(void);
void delayloop(unsigned long t)
{
unsigned int i;
if (t==0) return;
while (t--) for(i=0;i<K_DELAY; i++);
}
void delay_ms(unsigned long ms)
{
unsigned long i;
ms *= 1000;
for (i=0; i < ms; i++)
{
_delay_us(1);
}
}
void delay_us(unsigned int us)
{
unsigned long i;
for (i=0; i < us; i++)
{
_delay_us(1);
}
}
void blink(void)
{
for(int i=0;i<=10;i++)
{
outp(0xF0,PORTA);
delay_ms(50);
outp(0x0F,PORTA);
delay_ms(50);
}
}
void wait_until_key_pressed(void)
{
unsigned char temp1, temp2;
unsigned int i;
do {
temp1 = PIND; // read input
for(i=0;i<65535;i++);
temp2 = PIND; // read input
temp1 = (temp1 & temp2); // debounce input
} while ( temp1 & _BV(PIND2) );
loop_until_bit_is_set(PIND,PIND2); /* wait until key is released */
}
void wait_until_key_free(void)
{
unsigned char temp1, temp2;
unsigned int i;
do {
temp1 = PIND; // read input
for(i=0;i<65535;i++);
temp2 = PIND; // read input
temp1 = (temp1 & temp2); // debounce input
} while ( temp1 & _BV(PIND2) );
loop_until_bit_is_clear(PIND,PIND2);
}
SIGNAL(SIG_INTERRUPT0)
{
char pressedButtonIndex;
pressedButtonIndex = (PINB & 0x0F);
PORTA=0x00;
if(chkbit(PIND,2))
{
PORTA = pressedButtonIndex;
delay_ms(50);
}else{
PORTA=0x00;
PINB = 0x00;
}
tempPort = pressedButtonIndex;
//wait_until_key_free();
}
void int0_initial(void)
{
//SREG = 0x80;
MCUCR = (1<<ISC01) | (1<<ISC00);
GICR = (1<<INT0);
}
/*****************************************************************************/
int main(void)
{
unsigned char tempPort;
DDRA=0xFF;
PORTA=0x00;
DDRB=0x00;
PORTB=0x00;
DDRC=0x0F;
PORTC=0x00;
outp(0x0F,PORTA);
delay_ms(200);
outp(0xF0,PORTA);
delay_ms(200);
outp(0b00001100,PORTC);
delay_ms(200);
outp(0b00000011,PORTC);
delay_ms(200);
sei();
int0_initial();
while(1)
{
PORTC = tempPort;
// outp(tempPort,PORTC);
/*
if (chkbit(PINB,0))
{
PORTA = 0x01;
}
else {
PORTA = 0x02;
}
*/
}
return 0;
}
while(TRUE)
{
//while(usart_ready()) /* True if "Not" empty */
while(DataInReceiveBuffer()) /* True if "Not" empty */
{
c = usart_getc();
// Recieve "W"
if(c =='W') // Full Function Search DS1820 ID
{
start_meas();
for( diff = SEARCH_FIRST; diff != LAST_DEVICE; )
{
diff = w1_rom_search( diff, id );
usart_SendBcd(diff);
usart_puts( " = " );
for(unsigned char i = 0; i < 8; i++ ){
//sprintf( s, "%02X", id[i] );
//usart_s( s );
usart_SendBcd(id[i]);
}
usart_puts( " [*] " );
if( diff == PRESENCE_ERR ){
usart_puts( "No Sensor found \n" );
break;
}
if( diff == DATA_ERR ){
usart_puts( "Bus Error \n" );
break;
}
if( id[0] == 0x28 || id[0] == 0x10 )
{ // if have DS1820 sensor
usart_puts( "ID>>" );
for(unsigned char i = 0; i < 8; i++ ){
sprintf( s, "%02X ", id[i] );
usart_puts( s );
}
usart_puts( "\n\r" );
}
}
}
// Recieve "S"
if (c =='S') /* Single Search DS1820 by interbal Function */
{
lcd_gotoxy(0,0);
start_meas();
id_search();
}
// Recieve "X"
if (c =='X') /* Single Read DS1820 show to LCD */
{
lcd_clrscr();
lcd_gotoxy(0,0);
start_meas();
read_meas();
}
// Recieve "Y"
if(c =='Y') /* Single Read DS1820 show to USART */
{
start_meas();
read_meas_usart();
}
// Recieve "Z"
if (c =='Z') /* Continueus Read DS1820 show to LCD+USART */
{
lcd_clrscr();
do{
lcd_gotoxy(0,0);
start_meas();
read_meas();
start_meas();
read_meas_usart();
usart_puts(" \n\r");
delay_ms(1000);
}while(!usart_ready());
}
}/* end of check Command data buffer*/
}/*end of main While() loop */
/* while(1)
{
cbi(PortMAX3100,CS);
tempInt = read_MAX3100(0x80F1);
sbi(PortMAX3100,CS);
cbi(PortMAX3100,DIN);
cbi(PortMAX3100,CLK);
delay_us(CS_PULSE);
usart_puts("W1: ");
usart_16bitHex(tempInt);
usart_puts(" ::\n\r");
delay_ms(1000);
while(irqFlag = 0);
delay_ms(1000);
sbi(PortMAX3100,CS);
cbi(PortMAX3100,DIN);
cbi(PortMAX3100,CLK);
delay_us(CS_PULSE);
cbi(PortMAX3100,CS);
tx1 = read_MAX3100(0x8081);
sbi(PortMAX3100,CS);
cbi(PortMAX3100,DIN);
cbi(PortMAX3100,CLK);
usart_puts("W2: ");
usart_16bitHex(tx1);
usart_puts(" ::\n\r");
delay_us(CS_PULSE);
if((tx1 & 0x8000)==1)
cbi(PortMAX3100,CS);
tx1 = read_MAX3100(0x0000);
sbi(PortMAX3100,CS);
cbi(PortMAX3100,DIN);
cbi(PortMAX3100,CLK);
delay_us(CS_PULSE);
cbi(PortMAX3100,CS);
tx1 = read_MAX3100(0x8081);
sbi(PortMAX3100,CS);
cbi(PortMAX3100,DIN);
cbi(PortMAX3100,CLK);
delay_us(CS_PULSE);
cbi(PortMAX3100,CS);
tx1 = read_MAX3100(0x0000);
sbi(PortMAX3100,CS);
cbi(PortMAX3100,DIN);
cbi(PortMAX3100,CLK);
delay_us(CS_PULSE);
cbi(PortMAX3100,CS);
tx1 = read_MAX3100(0x0000);
sbi(PortMAX3100,CS);
cbi(PortMAX3100,DIN);
cbi(PortMAX3100,CLK);
delay_ms(1000);
}
*/
//******************************** END *******************************************//
/*
while(DataInReceiveBuffer())
{
c = usart_getc();
if(c =='A')
{
milseconds=0;
usart_puts("\n\r ## Recieve 'A' ## \n\r");
do{
if(milseconds==500)
{
milseconds=0;
usart_puts("\n\r -- 'A' ## \n\r");
}
}while(!DataInReceiveBuffer());
}
else if(c =='B') // Recieve "A" loop for read A?D ch-0
{
milseconds=0;
usart_puts("\n\r ## Recieve 'B' ## \n\r");
do{
if(milseconds==500)
{
milseconds=0;
usart_puts("\n\r -- 'B' ## \n\r");
}
}while(!DataInReceiveBuffer());
}// end Recieve "A"
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -