messier
Ban Chủ Nhiệm
Tình hình sau 3 buổi học code liên tiếp không có gì vui nên bữa tới lớp C12 sẽ đổi sang học vẽ mạch. Cụ thể là sẽ dùng phần mềm Eagle Cad để thiết kế một mạch nguồn linear đơn giản. Các bạn tải thêm các thư viện sau để sử dụng: Sparkfun.lbr, adafruit.lbr và PIF.lbr
Hướng dẫn sử dụng Eagle Cad các bạn có thể xem trước trên trang Sparkfun.
Ngoài ra các bạn download datasheet của các IC nguồn sau để xem trước (chân cẳng, hoạt động)
- LM317
- LM7805
Về module Analog các bạn không tìm ra link thì có thể download dưới đây:
Link down module: ChickyAnalog
BOM list: BOM
Source code đo nhiệt độ buổi học trước:
Source code PWM led dimming:
Source code PWM điều khiển LED RGB
Source code sử dụng capture để đo thời gian nhấn phím.
Hướng dẫn sử dụng Eagle Cad các bạn có thể xem trước trên trang Sparkfun.
Ngoài ra các bạn download datasheet của các IC nguồn sau để xem trước (chân cẳng, hoạt động)
- LM317
- LM7805
Về module Analog các bạn không tìm ra link thì có thể download dưới đây:
Link down module: ChickyAnalog
BOM list: BOM
Source code đo nhiệt độ buổi học trước:
Code:
/******************************************************************************
*
* www.payitforward.edu.vn
*
******************************************************************************/
/******************************************************************************
*
* C7 TRAINING
*
******************************************************************************/
/******************************************************************************
*
* Module : TEMPERATURE SENSOR
* Description : This file describes API functions that support for
* temperature sensor on MSP430G2553
*
* Tool : CCS 5.1
* Chip : MSP430G2553
* History : 04-11-2012
* Version : 1
*
* Author :
* Notes : To apply these functions, you must include the header file lcd.h and
* source file lcd.c to your project.
*
*
******************************************************************************/
/****************************************************************************
* IMPORT
******************************************************************************/
#include <msp430g2553.h>
/****************************************************************************
* DECLARATIONS
******************************************************************************/
unsigned char temp;
/*****************************************************************************
* SUBROUTINES
******************************************************************************/
//*****************************************************************************
// Initialization for ADC10 module
//*****************************************************************************
void ADC10_Init(void)
{
ADC10CTL0 = SREF_1 + ADC10SHT_3 + ADC10ON + ADC10IE + REFON;
/*
* Vr+ = Vref+
* ADC sample and hold time = 64 ADC clocks: When using the temperature
sensor, the sample period must be greater than 30 µs
* Turn on ADC10
* Enable ADC10 Interrupt
* Turn on reference generator
*/
ADC10CTL1 = INCH_10 + ADC10DIV_1 + ADC10SSEL_3 ;
/*
* Select Input chanel: Temperature Sensor
* ADC10DF = 0: The 10-bit conversion results are right justified
* ADC10 Clock divider: 4
* ADC10 clock source select: SMCLK
*/
}
/*****************************************************************************
* MAIN PROGRAM
******************************************************************************/
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //Stop Watchdog Timer
ADC10_Init(); //Initialize ADC10
// P2SEL = 0; //Turn off external crystal
// P2SEL2 = 0;
while (1)
{
ADC10CTL0 |= ENC + ADC10SC; //Enable ADC10, Start sample - conversion
_bis_SR_register(LPM0_bits + GIE); //Enter LPM0, wait for sample-conversion finish
_delay_cycles(50000);
}
}
/*****************************************************************************
* INTERRUPT PROGRAM
******************************************************************************/
#pragma vector = ADC10_VECTOR
__interrupt void ADC10_Interrupt(void)
{
temp=((ADC10MEM - 673) * 423) / 1024; //Calculate temperature
/*
* VTEMP=0.00355(TEMPC)+0.986
* Vref = 1.5V
*/
_bic_SR_register_on_exit(LPM0_bits); //Exit LPM0
}
/*****************************************************************************
* END
******************************************************************************/
Code:
#include <msp430.h>
void setup_timer(){
TA0CCR0 = 10000; // T = 10ms
// Duty cycles = 10%
// => CCR1/CCR0 = 10%
TA0CCR1 = 10;
TA0CCTL1 = OUTMOD_7; // Outmode 7: Reset/set
TA0CTL = TASSEL_2 + MC_1; // SMCLK, Up mode
}
void setup_port(){
// OUT1 -> P1.6 (xem datasheet)
P1SEL |= BIT6;
P1SEL2 &= ~BIT6;
P1DIR |= BIT6;
}
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
setup_port();
setup_timer();
_bis_SR_register(LPM0_bits); // Cho MCU vao che do Low power mode 0
return 0;
}
Code:
// R -> P1.2, G -> P2.1, B -> p2.4
#include <msp430.h>
#include <stdint.h>
char r,g,b;
void Wheel(uint16_t WheelPos, char *r, char *g, char *b)
{
switch(WheelPos / 128)
{
case 0:
*r = 127 - WheelPos % 128; // red down
*g = WheelPos % 128; // green up
*b = 0; // blue off
break;
case 1:
*g = 127 - WheelPos % 128; // green down
*b = WheelPos % 128; // blue up
*r = 0; // red off
break;
case 2:
*b = 127 - WheelPos % 128; // blue down
*r = WheelPos % 128; // red up
*g = 0; // green off
break;
}
}
void setup_timer(){
TA0CCTL1 = OUTMOD_7;
TA0CCR0 = 127;
TA0CTL = TASSEL_2 + MC_1;
TA1CCTL1 = OUTMOD_7;
TA1CCTL2 = OUTMOD_7;
TA1CCR0 = 127;
TA1CTL = TASSEL_2 + MC_1;
}
void setup_port(){
P1SEL |= BIT2;
P1SEL2 &= ~BIT2;
P1DIR |= BIT2;
P2SEL |= BIT1 + BIT4;
P2SEL2 &= ~(BIT1 + BIT4);
P2DIR |= BIT1 + BIT4;
}
/*
* main.c
*/
int main(void) {
uint16_t i;
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
setup_port();
TA0CCR1 = 0;
TA1CCR1 = 127;
TA1CCR2 = 127;
setup_timer();
while(1){
for(i = 0; i < 384; i++){
Wheel(i, &r, &g, &b);
TA0CCR1 = r;
TA1CCR1 = g;
TA1CCR2 = b;
__delay_cycles(20000);
}
}
return 0;
}
Code:
#include <msp430.h>
// Nut nhan -> P1.2 (timer 0 CCI1A)
void setup_timer(){
// Capture canh xuong, CCIA, cho phep ngat
TA0CCTL1 = CM_2 + CCIS_0 + CAP + CCIE;
TA0CTL = TASSEL_2 + MC_2 + ID_3;// SMCLK/8, Continuos mode
}
void setup_port(){
// CCI1A -> P1.2 (datasheet)
P1SEL |= BIT2;
P1SEL2 &= ~BIT2;
P1DIR &= ~BIT2;
// Chon tro keo len
P1REN |= BIT2;
P1OUT |= BIT2;
}
/*
* main.c
*/
int main(void) {
volatile int ccr1_store, delta_count;
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
setup_port();
while(1){
setup_timer();
_bis_SR_register(GIE+LPM0_bits);
// Luu thoi diem capture o canh xuong
ccr1_store = TA0CCR1;
_bis_SR_register(GIE+LPM0_bits);
// Thoi diem caputure canh len - thoi diem capture canh xuong
delta_count = TA0CCR1 - ccr1_store;
}
return 0;
}
#pragma vector=TIMER0_A1_VECTOR
__interrupt void timer0_a1_isr(){
switch(TA0IV){
case 0x02:
// Config capture canh len
TA0CCTL1 = CM_1 + CCIS_0 + CAP + CCIE;
_bic_SR_register_on_exit(LPM0_bits);
break;
case 0x04:
break;
case 0x0A:
break;
}
}