㈠ 求一份使用MSP4302553单片机做的样例代码,是调用光电门测速的,越快越好
//******************************************************************************
// HW UART(J4)!!!!!! 特别注意,板子上J4有2个跳线要竖放,设为HW UART模式
// ADC检测,并通过串口发送到PC
// MSP430G2533 Demo - USCI_A0, 9600 UART Echo ISR, DCO SMCLK
// http://jiwm.blog.163.com
// Baud rate divider with 1MHz = 1MHz/9600 = ~104.2
// ACLK = n/a, MCLK = SMCLK = CALxxx_1MHZ = 1MHz
//
// MSP430G2xx3
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | |
// | P1.2/UCA0TXD|------------>
// | | 9600 - 8N1
// | P1.1/UCA0RXD|<------------
//
// IAR Embedded Workbench Version: 5.40
//******************************************************************************
#include "msp430g2553.h"
long temp;
long IntDegF;
long IntDegC;
unsigned int i;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
ADC10CTL1 = INCH_10 + ADC10DIV_3; // Temp Sensor ADC10CLK/4
ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON + ADC10IE; //The ADC10OSC, generated internally,
//is in the 5-MHz range, but varies with indivial devices, supply voltage, and temperature. See the device-specific data sheet for the ADC10OSC specification.
DCOCTL = 0x00; // Set DCO 1MHz
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
P1SEL2 = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
UCA0CTL1 |= UCSSEL_2; // USCI clock source=SMCLK
UCA0BR0 = 104; // 1MHz 9600 Refer to Page 435 of X2xx Guide
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS0; // Molation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
__enable_interrupt(); // Enable interrupts.
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
//__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
for(;;)
{ ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
}
}
#pragma vector=USCIAB0RX_VECTOR //串口中断
__interrupt void USCI0RX_ISR(void)
{
if ((UCA0RXBUF == 'c')||(UCA0RXBUF == 'C')) // 'C' or 'c' received?
{
//IE2 |= UCA0TXIE; // Enable USCI_A0 TX interrupt
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
{
UCA0TXBUF=((IntDegC/10240)+48); //10位数
}
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
{
UCA0TXBUF=((IntDegC/1024)%10+48); //个位数
}
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
{
UCA0TXBUF=46; //.
}
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
{
UCA0TXBUF=((IntDegC*10/1024%10)+48); //小数
}
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
{
UCA0TXBUF=13; //换行
}
}
else
{
//IE2 |= UCA0TXIE; // Enable USCI_A0 TX interrupt
UCA0TXBUF = 13; //换行
}
}
// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR (void)
{
temp = ADC10MEM;
//IntDegC = ((temp - 673) * 423) / 1024; //-278~144 ,精度降低了
IntDegC = ((temp - 673) * 423); //改进后的
}
㈡ 单片机如何实现ucb通信功能
有专用的USB转换芯片,用单片机和它连接,由这个芯片和USB设备通信即可。比如CH374、PDIUSBD12等。
如果电路板太小,建议用自带USB功能的单片机。51系列中,C8051F的单片机有自带USB模块的。PIC、AVR的单片机也有类似功能,另外,网上也有关于直接用PIC或AVR单片机IO口进行软件模拟USB通信的程序。(用51实现软件模拟可能复杂点)。
㈢ 如何实现VC与单片机RS-232通讯控制
有c语言基础开发RS-232,用c++,MFC开发,(开发com使用简单c++语句就可以了,不难跟你用的c语言很相似)vb更容易实现同样功能,但是vc是大势所趋,看一下龚建伟的RS232串口设计,非常简单,最主要就是电话控件(串口的控件),书上面源代码有,买一本,串口助手实现的源码也有,也很容易,一个礼拜就看完,书上从最简单的用PC控制跑马灯讲解,放寒假可以练习,平时上课时间久别去弄了,高考要紧,想研究单片机或者更复杂的嵌入式上面,时间不够用,在没人带你的情况下要走的路也很曲折,在应试教育的情况下,先把高考这关过了。
㈣ MSP430系列单片机跟蓝牙串口模块相连的是哪个接口啊
msp430的15、16系列的 还有2系列的都有I2c接口 程序源代码 ti网站上有 直接下就可以 每一种都有相应的code library。
贴一个2274的i2c代码吧 希望对你有用
include "msp430x22x4.h"
unsigned int RxByteCtr;
unsigned int RxWord;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1DIR |= 0x01; // P1.0 output
P3SEL |= 0x06; // Assign I2C pins to USCI_B0
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0I2CSA = 0x4e; // Set slave address
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
IE2 |= UCB0RXIE; // Enable RX interrupt
TACTL = TASSEL_2 + MC_2; // SMCLK, contmode
while (1)
{
RxByteCtr = 2; // Load RX byte counter
UCB0CTL1 |= UCTXSTT; // I2C start condition
__bis_SR_register(CPUOFF + GIE); // Enter LPM0, enable interrupts
// Remain in LPM0 until all data
// is RX'd
if (RxWord < 0x1d00) // >28C?
P1OUT &= ~0x01; // No, P1.0 = 0
else
P1OUT |= 0x01; // Yes, P1.0 = 1
__disable_interrupt();
TACCTL0 |= CCIE; // TACCR0 interrupt enabled
__bis_SR_register(CPUOFF + GIE); // Enter LPM0, enable interrupts
// Remain in LPM0 until TACCR0
// interrupt occurs
TACCTL0 &= ~CCIE; // TACCR0 interrupt disabled
}
}
#pragma vector = TIMERA0_VECTOR
__interrupt void TA0_ISR(void)
{
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
// The USCIAB0TX_ISR is structured such that it can be used to receive any
// 2+ number of bytes by pre-loading RxByteCtr with the byte count.
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
RxByteCtr--; // Decrement RX byte counter
if (RxByteCtr)
{
RxWord = (unsigned int)UCB0RXBUF << 8; // Get received byte
if (RxByteCtr == 1) // Only one byte left?
UCB0CTL1 |= UCTXSTP; // Generate I2C stop condition
}
else
{
RxWord |= UCB0RXBUF; // Get final received byte,
// Combine MSB and LSB
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
}