eegmak
@eegmak

Почему код срабатывает только один раз?

//! Serial interface loopback test
//!
//! You have to short the TX and RX pins to make this program work

#![deny(unsafe_code)]
#![no_main]
#![no_std]

use panic_halt as _;

//use cortex_m::asm;

use nb::block;

use cortex_m_rt::entry;
use stm32f1xx_hal::{
    pac,
    timer::Timer,
    prelude::*,
    serial::{Config, Serial},
  //  timer::Timer,
};
use embedded_hal::digital::v2::OutputPin;
#[entry]
fn main() -> ! {
    // Get access to the device specific peripherals from the peripheral access crate
    let cp = cortex_m::Peripherals::take().unwrap();
    let p = pac::Peripherals::take().unwrap();

   // let cp = cortex_m::Peripherals::take().unwrap();
    // Take ownership over the raw flash and rcc devices and convert them into the corresponding
    // HAL structs
    let mut flash = p.FLASH.constrain();
    let mut rcc = p.RCC.constrain();

    // Freeze the configuration of all the clocks in the system and store the frozen frequencies in
    // `clocks`
    let clocks = rcc
            .cfgr
            .sysclk(48.mhz())
            .pclk1(24.mhz())
            .freeze(&mut flash.acr);

    // Prepare the alternate function I/O registers
    let mut afio = p.AFIO.constrain(&mut rcc.apb2);

    // Prepare the GPIOB peripheral
    let mut gpioa = p.GPIOA.split(&mut rcc.apb2);
    let mut gpiob = p.GPIOB.split(&mut rcc.apb2);
    let mut gpioc = p.GPIOC.split(&mut rcc.apb2);
    // USART1
    let tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh);
    let rx = gpioa.pa10;

    // USART1
    // let tx = gpiob.pb6.into_alternate_push_pull(&mut gpiob.crl);
    // let rx = gpiob.pb7;

    // USART2
    // let tx = gpioa.pa2.into_alternate_push_pull(&mut gpioa.crl);
    // let rx = gpioa.pa3;

    // USART3
    // Configure pb10 as a push_pull output, this will be the tx pin
    //let tx = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh);
    // Take ownership over pb11
    //let rx = gpiob.pb11;

    // Set up the usart device. Taks ownership over the USART register and tx/rx pins. The rest of
    // the registers are used to enable and configure the device.
    let mut serial = Serial::usart1(
        p.USART1,
        (tx, rx),
        &mut afio.mapr,
        Config::default().baudrate(115200.bps()),
        clocks,
        &mut rcc.apb2,
    );

    // Loopback test. Write `X` and wait until the write is successful.
    let sent = b'X';
    block!(serial.write(sent)).ok();

    // Read the byte that was just sent. Blocks until the read is complete
    //let received = block!(serial.read()).unwrap();

    // Since we have connected tx and rx, the byte we sent should be the one we received
    //assert_eq!(received, sent);

    // Trigger a breakpoint to allow us to inspect the values
    //asm::bkpt();

    // You can also split the serial struct into a receiving and a transmitting part
    //let (mut tx, mut rx) = serial.split();
    //let sent = b'Y';
    //block!(tx.write(sent)).ok();
    //let received = block!(rx.read()).unwrap();
    //assert_eq!(received, sent);
    //asm::bkpt();
    //let mut x = 24;
    let mut received;
    let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
    let mut ledws = gpiob.pb12.into_push_pull_output(&mut gpiob.crh);
    let mut timer = Timer::syst(cp.SYST, &clocks)
    .start_count_down(800000.hz());
    loop {
    	
    	received = block!(serial.read()).unwrap();
        block!(serial.write(received)).ok();
        if received == 48 {
        	led.set_high().unwrap();
        	let mut x1=64;
    	while x1>0 {
    		x1=x1-1;
        	let mut x = 24;
        	while x>0 {
        		x = x - 1;
        		ledws.set_high().unwrap();
        		ledws.set_low().unwrap();
        		block!(timer.wait()).unwrap();
        		block!(timer.wait()).unwrap();
        		block!(timer.wait()).unwrap();
        		block!(timer.wait()).unwrap();
        	}

        }}
        else if received== 49 {
        	led.set_low().unwrap();
        	let mut x1=64;
    	while x1>0 {
    		x1=x1-1;
        	let mut x = 24;
        	while x>0 {
        		x = x - 1;
        		ledws.set_high().unwrap();
        		block!(timer.wait()).unwrap();
        		ledws.set_low().unwrap();
        		block!(timer.wait()).unwrap();
        	}

        }
        } 
    	//block!(timer.wait()).unwrap();
        //led.set_high().unwrap();
        //block!(timer.wait()).unwrap();
        //led.set_low().unwrap();
    }
    
}

лампочка led переключается только один раз, до того как были добавлены циклы для ledws лампочка led переключалась по символу с уарта без проблем.. в чем ошибка?
  • Вопрос задан
  • 39 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы