써미터 10K
저항10K
V=IR
V=I*(R1+R2)10K+10K=20K
V=I*20000
I=5/20000A->1/4000=0.0025A=0.25mA
PWM=Pluse Width Modulator 신호의 가로폭 변화
ㅁ
ADC.c
#include "ADC.h"
void ADC_Init(void)
{
ADMUX=(0<<REFS1)|(0<<REFS0)|(0<<ADLAR)|(0<<MUX4)|(0<<MUX3)|(0<<MUX2)|(0<< MUX1)|(0<<MUX0);
ADCSRA=(1<<ADEN)|(1<<ADSC)|(0<<ADATE)|(0<<ADIF)|(0<<ADIE)|(1<<ADPS2)|(1<<ADPS1)|(0<<ADPS0);
ADCSRB=(0<<ACME)|(0<<MUX5)|(0<<ADTS2)|(0<<ADTS1) |(0<<ADTS0);
}
unsigned int ADC_Run0(void)
{
ADCSRA=ADCSRA|(1<<ADSC);
while(0!=(ADCSRA&(1<<ADSC)));
return ADCH;
}
ADC.h
#ifndef __ADC_H__
#define __ADC_H__
#include "main.h"
#endif //__ADC_H_
#ifndef __USART_H__
#define __USART_H__
#include "main.h"
#define OSC F_CPU
#define BAUD 115200 //bps
void USART0_Init(void);
unsigned char USART0_Rx(void);
void USART0_Tx(unsigned char ucData);
void USART0_STR(const char* cString);
void USART1_Init(void);
unsigned char USART1_Rx(void);
void USART1_Tx(unsigned char ucData);
void USART1_STR(const char* cString);
#endif //__USART_H__
#include "USART.h"
void USART0_Init(void)
{
UBRR0H=(unsigned long)((OSC/(16.0*BAUD))-0.5)>>8; //H - 8bit 0/1
UBRR0L=((OSC/(16.0*BAUD))-0.5); //반올림
//UCSR0A=(0<<RXC)|(0<<TXC)|(0<<UDRE)|(0<<FE)|(0<<DOR)|(0<<UPE)|(0<<U2X)|(0<<MPCM);
UCSR0B=(0<<RXCIE)|(0<<TXCIE) |(0<<UDRIE) |(1<<RXEN) |(1<<TXEN) |(0<<UCSZ2) |(0<<RXB8) |(0<<TXB8);
UCSR0C=(0<<UMSEL1)|(0<<UMSEL0)|(1<<UPM1)|(1<<UPM0)|(0<<USBS)|(1<<UCSZ1)|(1<<UCSZ0)|(0<<UCPOL);
}
unsigned char USART0_Rx(void) //반환값을 unsigned char로
{
while(0==((1<<RXC)&UCSR0A));
return UDR0;
}
void USART0_Tx(unsigned char ucData) //반환값을 unsigned char로
{
while(0==((1<<UDRE)&UCSR0A));
UDR0=ucData;
}
void USART0_STR(const char* cString)
{
while(0!=*cString)
{
USART0_Tx(*cString);
++cString;
}
}
void USART1_Init(void)
{
UBRR1H = (unsigned long)((OSC/(16.0*BAUD))-0.5)>>8;
UBRR1L= ((OSC/(16.0*BAUD))-0.5);
//UCSR1A=(0<<RXC)|(0<<TXC)|(0<<UDRE)|(0<<FE)|(0<<DOR)|(0<<UPE)|(0<<U2X)|(0<<MPCM);
UCSR1B = (0<<RXCIE)|(0<< TXCIE)|(0<< UDRIE)|(1<< RXEN)|(1<< TXEN) |(0<<UCSZ2)|(0<< RXB8)|(0<< TXB8);
UCSR1C=(0<<UMSEL1)|(0<<UMSEL0)|(1<<UPM1)|(1<<UPM0)|(1<<USBS)|(1<<UCSZ1)|(1<<UCSZ0)|(0<<UCPOL);
}
unsigned char USART1_Rx(void)
{
while(((1<<RXC) & UCSR1A)==0);
return UDR1;
}
void USART1_Tx(unsigned char ucData)
{
while(((1<<UDRE) & UCSR1A)==0);
UDR1=ucData;
}
void USART1_Str(const char *cString)
{
while(0 != (*cString))
{
USART1_Tx(*cString);
++cString;
}
}
#ifndef __TC0_H__
#define __TC0_H__
#define STOP 0
#define START 1
#include "main.h"
extern volatile unsigned int uiSec; // extern 외부, 어딘가엔 존재한다.
void TC0_Init(void);
void TC0_MSDelay(unsigned int uiDTime);
#endif //__TC0_H__
#include "TC0.h"
static volatile unsigned int uiMSec;
volatile unsigned int uiSec;
volatile unsigned int uiSState;
void TC0_Init(void)
{
TCCR0A=(0<<COM0A1)|(0<<COM0A0)|(0<<COM0B1)|(0<<COM0B0)|(0<<WGM01)|(0<<WGM00);
TCCR0B=(0<<FOC0A)|(0<<FOC0B)|(0<<WGM02)|(0<<CS02)|(0<<CS01)|(0<<CS00); //clkI/O/64 (From prescaler)
//TCNT0=0x00; 회로가 자동으로 숫자를 증가시키기에 주석처리함
OCR0A=250; // compare비교방식 선택 250을 한계 설정
//OCR0B=0x00; A와 비교하기 문에 주석처리
TIMSK0=(0<<OCIE0B)|(1<<OCIE0A)|(0<<TOIE0);
//TIFR0=0x00; TIFR은기본적으로 오버플로우 발생시 1로 set되었다가 차면 0으로 클리어, 강제로 0으로 클리어하려면 1
EICRA=(3<<ISC1) | (3<<ISC0)| (3<<ISC2);
EIMSK=(1<<INT1) | (1<<INT0)| (1<<INT2);
uiSState=STOP;
}
void __vector_21(void)
{
++uiMSec; // 1이 증가 0.001초가 지나감
if(uiMSec>=1000)
{
uiMSec=0;
++uiSec;
}
}
void __vector_1(void)
{
volatile unsigned int uiCnt;
if(STOP==uiSState)
{
TCCR0B = (0<<FOC0A) | (0<<FOC0B) | (0<<WGM02) | (0<<CS02) | (1<<CS01) | (1<<CS00);
uiSState=START;
}
else
{
TCCR0B = (0<<FOC0A) | (0<<FOC0B) | (0<<WGM02) | (0<<CS02) | (0<<CS01) | (0<<CS00);
uiSState=STOP;
}
for(uiCnt = 0; 30000>uiCnt ; ++uiCnt);
return;
}
void __vector_2(void)
{
volatile unsigned int uiCnt;
TCCR0B = (0<<FOC0A) | (0<<FOC0B) | (0<<WGM02) | (0<<CS02) | (0<<CS01) | (0<<CS00);
uiSec=0;
uiSState=STOP;
for(uiCnt = 0; 30000>uiCnt ; ++uiCnt);
return;
}
void __vector_3(void)
{
volatile unsigned int uiCnt;
uiSec=uiSec+60;
for(uiCnt = 0; 30000>uiCnt ; ++uiCnt);
return;
}
void TC0_MSDelay(unsigned int uiDTime)
{
if(uiDTime>1000)
{
uiDTime=1000;
}
uiMSec=0;
while(uiMSec<uiDTime);
}
#include "main.h"
#include "lcd.h"
#include "TC0.h"
#include "USART.h"
#include "ADC.h"
int main(void)
{
unsigned char ucData;
char caVal[]="0000";
unsigned int uiVal;
LCD_Init();
USART0_Init();
ADC_Init();
USART0_STR("AVRTEST");
while(1)
{
uiVal=ADC_Run0();
caVal[0]='0'+(uiVal/1000);
caVal[1]='0'+((uiVal%1000)/100);
caVal[2]='0'+((uiVal%100)/10);
caVal[3]='0'+(uiVal%10);
LCD_STR(caVal);
USART0_STR(caVal);
LCD_Inst(LCD_HOME);
}
return 0;
}
#ifndef __MAIN_H__
#define __MAIN_H__
void Init(void);
void Port_Init(void);
void INT_Init(void);
#define PINA (*((volatile unsigned char *)0x20))
#define DDRA (*((volatile unsigned char *)0x21))
#define PORTA (*((volatile unsigned char *)0x22))
#define PINC (*((volatile unsigned char *)0x26))
#define DDRC (*((volatile unsigned char *)0x27))
#define PORTC (*((volatile unsigned char *)0x28))
#define EICRA (*((volatile unsigned char *)0x69))
#define EICRB (*((volatile unsigned char *)0x6A))
#define EIMSK (*((volatile unsigned char *)0x3D))
#define SREG (*((volatile unsigned char *)0x5F))
#define TCCR0A (*((volatile unsigned char *)0x44))
#define TCCR0B (*((volatile unsigned char *)0x45))
#define TCNT0 (*((volatile unsigned char *)0x46))
#define OCR0A (*((volatile unsigned char *)0x47))
#define OCR0B (*((volatile unsigned char *)0x48))
#define TIMSK0 (*((volatile unsigned char *)0x6E))
#define TIFR0 (*((volatile unsigned char *)0x35))
#define COM0A1 7
#define COM0A0 6
#define COM0B1 5
#define COM0B0 4
#define WGM01 1
#define WGM00 0
#define FOC0A 7
#define FOC0B 6
#define WGM02 3
#define CS02 2
#define CS01 1
#define CS00 0
#define OCIE0B 2
#define OCIE0A 1
#define TOIE0 0
#define OCF0B 2
#define OCF0A 1
#define TOV0 0
#define INT7 7
#define INT6 6
#define INT5 5
#define INT4 4
#define INT3 3
#define INT2 2
#define INT1 1
#define INT0 0
#define ISC7 6
#define ISC6 4
#define ISC5 2
#define ISC4 0
#define ISC3 6
#define ISC2 4
#define ISC1 2
#define ISC0 0
#define UBRR0H (*((volatile unsigned char *)0xC5))
#define UBRR0L (*((volatile unsigned char *)0xc4))
#define UBRR1H (*((volatile unsigned char *)0xCD))
#define UBRR1L (*((volatile unsigned char *)0xCC))
#define UBRR2H (*((volatile unsigned char *)0xD5))
#define UBRR2L (*((volatile unsigned char *)0xD4))
#define UBRR3H (*((volatile unsigned char *)0x135))
#define UBRR3L (*((volatile unsigned char *)0x134))
#define UDR0 (*((volatile unsigned char *)0xC6))
#define UDR1 (*((volatile unsigned char *)0xCE))
#define UDR2 (*((volatile unsigned char *)0xD6))
#define UDR3 (*((volatile unsigned char *)0x136))
#define UCSR0A (*((volatile unsigned char *)0xC0))
#define UCSR0B (*((volatile unsigned char *)0xc1))
#define UCSR0C (*((volatile unsigned char *)0xC2))
#define UCSR1A (*((volatile unsigned char *)0xC8))
#define UCSR1B (*((volatile unsigned char *)0xC9))
#define UCSR1C (*((volatile unsigned char *)0xCA))
#define UCSR2A (*((volatile unsigned char *)0xD0))
#define UCSR2B (*((volatile unsigned char *)0xD1))
#define UCSR2C (*((volatile unsigned char *)0xD2))
#define UCSR3A (*((volatile unsigned char *)0x130))
#define UCSR3B (*((volatile unsigned char *)0x131))
#define UCSR3C (*((volatile unsigned char *)0x132))
#define RXC 7
#define TXC 6
#define UDRE 5
#define FE 4
#define DOR 3
#define UPE 2
#define U2X 1
#define MPCM 0
#define RXCIE 7
#define TXCIE 6
#define UDRIE 5
#define RXEN 4
#define TXEN 3
#define UCSZ2 2
#define RXB8 1
#define TXB8 0
#define UMSEL1 7
#define UMSEL0 6
#define UPM1 5
#define UPM0 4
#define USBS 3
#define UCSZ1 2
#define UCSZ0 1
#define UCPOL 0
//ADC
#define ADMUX (*((volatile unsigned char *)0x7c))
#define REFS1 7
#define REFS0 6
#define ADLAR 5
#define MUX4 4
#define MUX3 3
#define MUX2 2
#define MUX1 1
#define MUX0 0
#define ADCSRB (*((volatile unsigned char *)0x7B))
#define ACME 6
#define MUX5 3
#define ADTS2 2
#define ADTS1 1
#define ADTS0 0
#define ADCSRA (*((volatile unsigned char *)0x7A))
#define ADEN 7
#define ADSC 6
#define ADATE 5
#define ADIF 4
#define ADIE 3
#define ADPS2 2
#define ADPS1 1
#define ADPS0 0
#define ADCL (*((volatile unsigned char *)0x78))
#define ADC (*((volatile unsigned int*)0x78))
#define ADCH (*((volatile unsigned char *)0x79))
//ADLAR = 0
/*
#define ADC7 15
#define ADC6 14
#define ADC5 13
#define ADC4 12
#define ADC3 11
#define ADC2 10
#define ADC1 9
#define ADC0 8
#define ADC9 9
#define ADC8 8
//ADLAR = 1
#define ADCL (*((volatile unsigned char *)0x79))
#define ADCH (*((volatile unsigned char *)0x78))
#define ADC9 15
#define ADC8 14
#define ADC7 13
#define ADC6 12
#define ADC5 11
#define ADC4 10
#define ADC3 9
#define ADC2 8
#define ADC1 15
#define ADC0 14
*/
#define DIDR0 (*((volatile unsigned char *)0x7E))
#define ADC7D 7
#define ADC6D 6
#define ADC5D 5
#define ADC4D 4
#define ADC3D 3
#define ADC2D 2
#define ADC1D 1
#define ADC0D 0
#define DIDR2 (*((volatile unsigned char *)0x7D))
#define ADC15D 7
#define ADC14D 6
#define ADC13D 5
#define ADC12D 4
#define ADC11D 3
#define ADC10D 2
#define ADC9D 1
#define ADC8D 0
//
#define sei() __asm__ __volatile__ ("sei" ::) //7번비트 1로
#define sleep() __asm__ __volatile__ ( "sleep" "\n\t" :: )
void __vector_1(void) __attribute__((signal, used, externally_visible));
void __vector_2(void) __attribute__((signal, used, externally_visible));
void __vector_3(void) __attribute__((signal, used, externally_visible));
void __vector_21(void) __attribute__((signal, used, externally_visible));
#endif //__MAIN_H__
'코스웨어 > 16년 스마트컨트롤러' 카테고리의 다른 글
2016_03_30_환경변수_업무일지 (0) | 2016.03.30 |
---|---|
20160329_김도관_업무일지_유닉스및네트워크_C언어기초 (2) | 2016.03.30 |
20160324_박진한_업무일지_펌웨어 실습 (0) | 2016.03.30 |
2016-03-29.강정수.디버그활용 (0) | 2016.03.30 |
20160325_업무일지_정우민_로봇제어_파일의접근 (0) | 2016.03.29 |
20160329_박진한_업무일지_로봇제어_환경변수 (1) | 2016.03.29 |
20160329_장진웅_업무일지_로봇제어_환경변수 (0) | 2016.03.29 |
수업 초반에 했던 설정 (0) | 2016.03.29 |